标签:equals 控制器 ota mode 返回 基类 值类型 key cep
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用
1.@ModelAttribute注释void返回值的方法
@Controller  
public class HelloModelController {  
   
    @ModelAttribute   
    public void populateModel(@RequestParam String abc, Model model) {    
       model.addAttribute("attributeName", abc);    
    }    
  
    @RequestMapping(value = "/helloWorld")    
    public String helloWorld() {    
       return "helloWorld.jsp";    
    }   
}  
访问helloWorld方法时,会先调用populateModel方法,将页面参数abc(/helloWorld.html?abc=text)放到model的attributeName属性中,在视图中可以直接访问。
jsp页面页面如下:
<body>
<c:out value="${attributeName}"></c:out>
</body>
2.@ModelAttribute注释返回具体类的方法
@Controller  
public class Hello2ModelController {  
      
    @ModelAttribute   
    public User populateModel() {    
       User user=new User();  
       user.setAccount("ray");  
       return user;  
    }    
    @RequestMapping(value = "/helloWorld2")    
    public String helloWorld() {    
       return "helloWorld.jsp";    
    }    
}  
请求 http://localhost:8080/test/helloWorld2.html时,先populateModel方法,返回User对象,model属性的名称没有指定,
它由返回类型隐含表示,如这个方法返回User类型,那么这个model属性的名称是user。
<c:out value="${user.account}"></c:out>
也可以指定属性名称
@Controller  
public class Hello2ModelController {  
      
    @ModelAttribute(value="myUser")  
    public User populateModel() {    
       User user=new User();  
       user.setAccount("ray");  
       return user;  
    }    
    @RequestMapping(value = "/helloWorld2")    
    public String helloWorld(Model map) {    
       return "helloWorld.jsp";    
    }    
}  
<c:out value="${myUser.account}"></c:out>
对象合并:
@Controller  
public class Hello2ModelController {  
      
    @ModelAttribute  
    public User populateModel() {    
       User user=new User();  
       user.setAccount("ray");  
       return user;  
    }    
      
    @RequestMapping(value = "/helloWorld2")    
    public String helloWorld(User user) {  
        user.setName("老王");  
       return "helloWorld.jsp";    
    }    
}  
对象合并指定对象名称:
@Controller  
public class Hello2ModelController {  
      
    @ModelAttribute("myUser")  
    public User populateModel() {    
       User user=new User();  
       user.setAccount("ray");  
       return user;  
    }    
      
    @RequestMapping(value = "/helloWorld2")    
    public String helloWorld(@ModelAttribute("myUser") User user) {  
        user.setName("老王");  
       return "helloWorld.jsp";    
    }    
}  
这样在jsp中可以使用如下方式访问
<c:out value="${myUser.name}"></c:out>
<c:out value="${myUser.account}"></c:out>
3.通过此特性控制权限.
我们可以在基类方法中写此注解,需要控制权限的控制器,继承控制器就可以了。
public class BaseController {  
      
    @ModelAttribute  
    public void populateModel() throws Exception {    
       SysUser user=ContextUtil.getCurrentUser();  
       if(user.getAccount().equals("admin")){  
           throw new Exception("没有权限");  
       }  
    }    
}  
@Controller  
public class Hello2ModelController extends BaseController {  
      
    @RequestMapping(value = "/helloWorld2")    
    public String helloWorld(@ModelAttribute("myUser") User user) {  
        user.setName("老王");  
       return "helloWorld.jsp";    
    }    
}
控制权限的方法有很多,比如通过过滤器等
总结:
@ModelAttribute具有如下三个作用:
①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,
从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时使用。
其实@ModelAttribute此处对于供视图页面展示来说与model.addAttribute("attributeName", abc);功能类似。
public String test(@ModelAttribute("user") UserModel user)
此处多了一个注解@ModelAttribute("user"),它的作用是将该绑定的命令对象以“user”为名称添加到模型对象中供视图页面展示使用。
我们此时可以在视图页面使用${user.username}来获取绑定的命令对象的属性。
②暴露@RequestMapping 方法返回值为模型数据:
放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。
public @ModelAttribute("user2") UserModel test3(@ModelAttribute("user2") UserModel user)
大家可以看到返回值类型是命令对象类型,而且通过@ModelAttribute("user2")注解,此时会暴露返回值到模型数据( 名字为user2 ) 中供视图展示使用
@ModelAttribute 注解的返回值会覆盖@RequestMapping 注解方法中的@ModelAttribute 注解的同名命令对象
③暴露表单引用对象为模型数据:
放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用对象,如注册时需要选择的所在城市等,
而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加到模型对象中,用于视图页面展示时使用;
标签:equals 控制器 ota mode 返回 基类 值类型 key cep
原文地址:http://www.cnblogs.com/shen199103/p/7077073.html