Spring MVC @ModelAttribute注解使用总结
原文地址:点击
@ModelAttribute注解用于将请求参数绑定到Model对象,位于spring-web的jar中,类路径为:org.springframework.web.bind.annotation.ModelAttribute.
@ModelAttribute注解只有一个属性:value,类型:String,是否必要:否,说明:绑定的属性名称(key)
常用写法如下:
-
@ModelAttribute(
"username")
-
@ModelAttribute(value=
"username")
-
@ModelAttribute
使用方法整理如下:
1.测试准备
1.1使用的测试页面loginUI.jsp
-
<form id="loginForm" action="${pageContext.request.contextPath}/modelAttr/login.do" method="post">
-
<table>
-
<tr>
-
<td>用户名
</td>
-
<td>
<input type="text" name="username">
</td>
-
</tr>
-
<tr>
-
<td>密码
</td>
-
<td>
<input type="password" name="pwd">
</td>
-
</tr>
-
<tr>
-
<td colspan="2">
-
<input type="submit" value="登录">
-
</td>
-
</tr>
-
</table>
-
</form>
1.2测试结果页面result.jsp
-
<h1>username:${requestScope.username}
</h1>
-
<h1>pwd:${requestScope.pwd}
</h1>
-
<h1>username:${requestScope.user.username}
</h1>
-
<h1>pwd:${requestScope.user.pwd}
</h1>
1.3测试进入login.jsp的控制器
-
@Controller
-
@RequestMapping(
"/testModelAttr")
-
public
class TestModelAttributeController {
-
-
/**
-
* Description:@ModelAttrutibe测试页面
-
* @date 2017年4月14日
-
* @return 测试页面
-
* @throws Exception
-
* <p>Description:</p>
-
*/
-
@RequestMapping(
"/loginUI")
-
public String loginUI() throws Exception{
-
return
"modelAttribute/loginUI";
-
}
-
}
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有值
测试代码:
-
@ModelAttribute
-
public void userModel(Model model, @RequestParam("username") String username, @RequestParam("pwd") String pwd) {
-
model.addAttribute(
"username", username);
-
model.addAttribute(
"pwd", pwd);
-
}
-
-
@RequestMapping(
"/login")
-
public String login(Model model) throws Exception{
-
//测试是否包含username属性
-
System.out.println(model.containsAttribute(
"username"));
-
System.out.println(model.containsAttribute(
"pwd"));
-
-
return
"modelAttribute/result";
-
}
2.2方式二:@ModelAttribute("xxx")注释返回具体类的方法
这种情况@ModelAttribute的value值"xxx"作为model的attributeName,userModel方法的返回值作为attributeName属性的值;控制台打印true、false,result.jsp页面中requestScope.username有值
测试代码:
-
@ModelAttribute(value=
"username")
-
public String userModel(@RequestParam("username") String username) {
-
return username;
-
}
-
-
@RequestMapping(
"/login")
-
public String login(Model model) throws Exception{
-
//测试是否包含username属性
-
System.out.println(model.containsAttribute(
"username"));
-
System.out.println(model.containsAttribute(
"pwd"));
-
-
return
"modelAttribute/result";
-
}
2.3.方式三:@ModelAttribute注释返回具体类的方法
这种情况userModel方法的返回类型User类(取首字母小写)作为model的attributeName,返回值作为attributeName属性的值;控制台打印false、false、true,result.jsp页面中requestScope.user.username、requestScope.user.pwd有值
测试代码:
-
@ModelAttribute
-
public User userModel(@RequestParam("username") String username, @RequestParam("pwd") String pwd) {
-
return
new User(username, pwd);
-
}
-
-
@RequestMapping(
"/login")
-
public String login(Model model) throws Exception{
-
//测试是否包含username属性
-
System.out.println(model.containsAttribute(
"username"));
-
System.out.println(model.containsAttribute(
"pwd"));
-
System.out.println(model.containsAttribute(
"user"));
-
-
return
"modelAttribute/result";
-
}
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
测试代码:
-
@ModelAttribute(value=
"username")
-
@RequestMapping(
"/login")
-
public String login(@RequestParam("username") String username) throws Exception{
-
System.out.println(username);
-
return username;
-
}
3.注:
(1).@ModelAttribute注释的方法会在Controller中的其他方法执行前被调用,所以在一个Controller中映射多个URL时,需要注意;
(2).一般来说,@ModelAttribute指定value属性后,value的值作为model的attributeName,对应的方法返回值作为attributeName属性的值;若未指定value属性,且有返回类型,则取返回类(首字母小写)作为attributeName,返回值作为attributeName属性的值。大概的流程如下图:

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

被折叠的 条评论
为什么被折叠?



