Spring 梳理-传递模型数据到视图

  1. 传递model,返回string,不指定返回key
    1. package com.jt;
      import com.myOrg.DeptUserDomain;
      
      @Controller
      @RequestMapping(value="/FirstControl")
      public class HelloControl {
          @RequestMapping("/viewdud")
          public String viewDeptUser(Model model){
              DeptUserDomain dud=new DeptUserDomain();
              dud.setId("1");
              dud.setName("zs");
              dud.setSalary(10000);
              dud.setHireDate(new Date(100));
              model.addAttribute(dud);
              return "deptUser";
          }
      }

       

    2.  
      <body>
      viewDeptUser<br>
      ${deptUserDomain.id}<br>
      </body>

       

  2. 传递model,返回string,指定返回key
    1.        @RequestMapping("/viewdud")
          public String viewDeptUser(Model model){
              DeptUserDomain dud=new DeptUserDomain();
              dud.setId("1");
              dud.setName("zs");
              dud.setSalary(10000);
              dud.setHireDate(new Date(100));
              model.addAttribute("data",dud);
                
              return "deptUser";
          }

       

    2. <body>
      viewDeptUser<br>
      ${data.id}<br>
      </body>

       

  3. 传递map,返回string,必须指定返回key。(可避免被Spring类型的Model污染代码)
    1.     @RequestMapping("viewdudByMap")
          public String viewDeptUserByMap(Map map){
              DeptUserDomain dud=new DeptUserDomain();
              dud.setId("1");
              dud.setName("zs");
              dud.setSalary(10000);
              map.put("dataByMap",dud);
              return "deptUser";
          }
      <body>
      viewDeptUser<br>
      ${dataByMap.id}<br>
      </body>
        
  4. 传递空,返回data
    1. @Controller
      @RequestMapping(value="/FirstControl")
      public class HelloControl {
       @RequestMapping("/viewdudByReturn")
       public DeptUserDomain viewDeptUserByReturn(){
        DeptUserDomain dud=new DeptUserDomain();
        dud.setId("1");
        dud.setName("zs");
        dud.setSalary(10000);
        return dud;
       }
      }
       系统自动根据请求路径进行推断设置,视图名称为viewdudByReturn,jsp路径为 /WEB-INF/FirstControl/viewdudByReturn.jsp ,
      相当于返回的String为“/FirstControl/viewdudByReturn

      <body> viewDeptUser<br> ${deptUserDomain.id}<br> </body>

       

  

posted on 2018-10-03 18:14  手握太阳  阅读(129)  评论(0编辑  收藏  举报

导航