SpringMVC @ModelAttribute详解

@ModelAttribute一个具有如下三个作用:

①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑

定流程,而且自动暴露为模型数据用于视图页面展示时使用;

②暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用

对象,如注册时需要选择的所在城市等,而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加

到模型对象中,用于视图页面展示时使用;

③暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为

模型数据,用于视图页面展示时使用。

被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。

  我们编写控制器代码时,会将保存方法独立成一个控制器也是如此。

扫描二维码关注公众号,回复: 9009428 查看本文章

1、注释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.ht?abc=text)放到model的attributeName属性中,在视图中可以直接访问。

jsp页面页面如下:

复制代码

<%@ page language="java" contentType="text/html; charset=utf-8"
     pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
</head>
<body>
    <c:out value="${attributeName}"></c:out>
</body>
</html>

复制代码

2、注释返回具体类的方法

复制代码

@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时,首先访问populateModel方法,返回User对象,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回User类型,那么这个model属性的名称是user。这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。

jsp 中如下访问:

<c:out value="${user.account}"></c:out>

 

3、也可以指定属性名称

复制代码

@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";  
    }  
}

复制代码

jsp中如下访问:

<c:out value="${myUser.account}"></c:out>

4、对象合并

复制代码

@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";  
    }  
}

复制代码

  这个在编写代码的时候很有用处,比如在更新的时候,我们可以现在populateModel方法中根据ID获取对象,然后使用spring mvc的自动组装功能,组装User对象,这样在客户端提交了值的属性才会被组装到对象中。

  比如:User对象,首先从数据库中获取此对象,客户端表单只有account属性,提交时就只会改变account属性。

5、对象合并指定对象名称

复制代码

@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>

6、通过此特性控制权限

  我们可以在基类方法中控制写此注解,需要控制权限的控制器,继承控制器就可以了。

复制代码

public class BaseController {
   
    @ModelAttribute
    public void populateModel() throws Exception {  
       SysUser user=ContextUtil.getCurrentUser();
       if(user.getAccount().equals("admin")){
           throw new Exception("没有权限");
       }
    }  
}

复制代码

  需要控制权限的类继承BaseController

复制代码

@Controller
public class Hello2ModelController extends BaseController {
    
   @RequestMapping(value = "/helloWorld2")  
   public String helloWorld(@ModelAttribute("myUser") User user) {
      user.setName("老王");
      return "helloWorld.jsp";  
   }  
}

复制代码

  这样就可以控制权限了,当然控制权限的方法有很多,比如通过过滤器等。这里只是提供一种思路。

发布了685 篇原创文章 · 获赞 632 · 访问量 126万+

猜你喜欢

转载自blog.csdn.net/zhangchen124/article/details/104190171