Spring MVC @ModelAttribute注解总结

本文总结了Spring MVC中@ModelAttribute注解的使用。该注解用于将请求参数绑定到Model对象,介绍了其属性及常用写法。详细阐述了四种使用方式,包括注释void返回值方法、注释返回具体类的方法等,并给出测试结果和代码。还提醒了使用时的注意事项。

Spring MVC @ModelAttribute注解使用总结

原文地址:点击

@ModelAttribute注解用于将请求参数绑定到Model对象,位于spring-web的jar中,类路径为:org.springframework.web.bind.annotation.ModelAttribute.

@ModelAttribute注解只有一个属性:value,类型:String,是否必要:否,说明:绑定的属性名称(key)

常用写法如下:


 
  1. @ModelAttribute( "username")
  2. @ModelAttribute(value= "username")
  3. @ModelAttribute

使用方法整理如下:

1.测试准备

1.1使用的测试页面loginUI.jsp


 
  1. <form id="loginForm" action="${pageContext.request.contextPath}/modelAttr/login.do" method="post">
  2. <table>
  3. <tr>
  4. <td>用户名 </td>
  5. <td> <input type="text" name="username"> </td>
  6. </tr>
  7. <tr>
  8. <td>密码 </td>
  9. <td> <input type="password" name="pwd"> </td>
  10. </tr>
  11. <tr>
  12. <td colspan="2">
  13. <input type="submit" value="登录">
  14. </td>
  15. </tr>
  16. </table>
  17. </form>
1.2测试结果页面result.jsp


 
  1. <h1>username:${requestScope.username} </h1>
  2. <h1>pwd:${requestScope.pwd} </h1>
  3. <h1>username:${requestScope.user.username} </h1>
  4. <h1>pwd:${requestScope.user.pwd} </h1>
1.3测试进入login.jsp的控制器


 
  1. @Controller
  2. @RequestMapping( "/testModelAttr")
  3. public class TestModelAttributeController {
  4. /**
  5. * Description:@ModelAttrutibe测试页面
  6. * @date 2017年4月14日
  7. * @return 测试页面
  8. * @throws Exception
  9. * <p>Description:</p>
  10. */
  11. @RequestMapping( "/loginUI")
  12. public String loginUI() throws Exception{
  13. return "modelAttribute/loginUI";
  14. }
  15. }

2.使用方式

2.1.方式一:@ModelAttribute注释void返回值的方法

这种情况需要通过model.addAttribute(key, value)方法绑定数据,在login方法通过model.containsAttribute("username")测试userModel方法中是否起作用,因为@ModelAttribute注释的方法在请求的方法之前调用。之后跳转到result.jsp中,查看username、pwd是否能获取到。测试结果:控制台打印true、true,result.jsp页面中requestScope.username、requestScope.pwd有值

测试代码:


 
  1. @ModelAttribute
  2. public void userModel(Model model, @RequestParam("username") String username, @RequestParam("pwd") String pwd) {
  3. model.addAttribute( "username", username);
  4. model.addAttribute( "pwd", pwd);
  5. }
  6. @RequestMapping( "/login")
  7. public String login(Model model) throws Exception{
  8. //测试是否包含username属性
  9. System.out.println(model.containsAttribute( "username"));
  10. System.out.println(model.containsAttribute( "pwd"));
  11. return "modelAttribute/result";
  12. }
2.2方式二:@ModelAttribute("xxx")注释返回具体类的方法

这种情况@ModelAttribute的value值"xxx"作为model的attributeName,userModel方法的返回值作为attributeName属性的值;控制台打印true、false,result.jsp页面中requestScope.username有值

测试代码:


 
  1. @ModelAttribute(value= "username")
  2. public String userModel(@RequestParam("username") String username) {
  3. return username;
  4. }
  5. @RequestMapping( "/login")
  6. public String login(Model model) throws Exception{
  7. //测试是否包含username属性
  8. System.out.println(model.containsAttribute( "username"));
  9. System.out.println(model.containsAttribute( "pwd"));
  10. return "modelAttribute/result";
  11. }
2.3.方式三:@ModelAttribute注释返回具体类的方法

这种情况userModel方法的返回类型User类(取首字母小写)作为model的attributeName,返回值作为attributeName属性的值;控制台打印false、false、true,result.jsp页面中requestScope.user.username、requestScope.user.pwd有值

测试代码:


 
  1. @ModelAttribute
  2. public User userModel(@RequestParam("username") String username, @RequestParam("pwd") String pwd) {
  3. return new User(username, pwd);
  4. }
  5. @RequestMapping( "/login")
  6. public String login(Model model) throws Exception{
  7. //测试是否包含username属性
  8. System.out.println(model.containsAttribute( "username"));
  9. System.out.println(model.containsAttribute( "pwd"));
  10. System.out.println(model.containsAttribute( "user"));
  11. return "modelAttribute/result";
  12. }
2.4.方式四:@ModelAttribute("xxx")和@RequestMapping("/yyy")同时注释的有返回值的方法

这种情况下@ModelAttribute("xxx")的属性值作为model的attributeName,login方法返回值作为attributeName属性的值;@RequestMapping("/yyy")的yyy作为请求路径,同时也作为视图名,即跳转页面的名称;如请求路径为localhost:8080/xxx/modelAttr/login.do,那么跳转页面为localhost:8080/xxx/WEB-INF/jsp/modelAttr/login.jsp

测试代码:


 
  1. @ModelAttribute(value= "username")
  2. @RequestMapping( "/login")
  3. public String login(@RequestParam("username") String username) throws Exception{
  4. System.out.println(username);
  5. return username;
  6. }

3.注:

(1).@ModelAttribute注释的方法会在Controller中的其他方法执行前被调用,所以在一个Controller中映射多个URL时,需要注意;

(2).一般来说,@ModelAttribute指定value属性后,value的值作为model的attributeName,对应的方法返回值作为attributeName属性的值;若未指定value属性,且有返回类型,则取返回类(首字母小写)作为attributeName,返回值作为attributeName属性的值。大概的流程如下图:







评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值