SpringMVC POJO入参过程分析
SpringMVC确定目标方法POJO类型的入参过程
1、确认一个key:
(1)、若目标方法的POJO类型的参数没有使用@ModelAttribute作为修饰,则key为POJO类名第一个字母的小写
(2)、若使用@ModelAttribute来修饰,则key为@ModelAttibute注解的value属性值。
@RequestMapping("/updateUser")
public String update(User user){
这里没有使用@ModelAttribute修饰,所有对应的key为POJO类名首字母小写,即为:user
public String update(@ModelAttribute("abc") User user)
这里使用了@ModelAttribute来修饰POJO,那么对应的可以为@ModelAttribute的value而值,即为:abc
2、在implicitModel中查找key对应的对象。若存在,则作为参数传入
(1)、若在@ModelAttibute标记的方法中在Map中保存过POJO,且key和1中确认的key一致,则会获取。例如:
package com.proc; import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
public class UserController { @ModelAttribute
public void getUser(@RequestParam(required=false) Integer id,Map map){
if(id!=null){
User user=new User();
user.setId(1);
user.setUsername("caoyc");
user.setAge(18);
user.setPassword("123456");
map.put("user", user);
}
} @RequestMapping("/updateUser")
public String update(@ModelAttribute("user") User user){
System.out.println("更新User对象为:"+user);
return "success";
}
}
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> </head> <body>
<form action="updateUser" method="post">
<input type="hidden" name="id" value="1">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username" value="caoyc"/></td>
</tr>
<tr>
<td>年龄:</td><td><input type="text" name="age" value="12"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
结果在控制台中输出:User [id=1, username=caoyc, password=123456, age=12]
其中原因是:在方法/updateUser时对应的方法update( User user)时,先要执行@ModelAttribute标记的方法,而在@ModelAttribute中向implicitModel中保存了一个key为user的POJO对象。而此时update对象中参数的key为POJO类名首字母小写,也是user,也就是在implicitModel中存在该key和对应的POJO对象。那么就会以该对象作为原始值。而JSP页面中传入过来的POJO对象属性不为空且有变化的属性来修改原始值。
原始对象:User [id=1, username=caoyc, password=123456, age=18]
JSP中 :User[id=1, username=caoyc, password=null, age=12]
这里需要修改的属性为age。在原始值得基础上修改了将age修改为12
所以最终在implicitModel中key值为user的POJO对象为:User [id=1, username=caoyc, password=123456, age=12]
3、若在implicitModel中不存在key对应的对象,则检查当前的Handler是否使用了@SessionAttributes注解修饰,若使用了该注解,且@SessionAttribute注解的value属性值中包含了key,则会从HttpSession中获取key所对应的value值,若存在则直接传入到目标方法的入参中,若不存在则将抛出异常。
Java代码
@SessionAttributes(value={"user"})
@Controller
public class UserController { @RequestMapping("/updateUser")
public String update(User user){
//System.out.println("更新User对象为:"+user);
return "success";
}
}
此时,如果session中没有key为user的值,那么只需/updateUser时会抛出异常。因为在只需目标方法时,发现类有@SessionAttributes注解,且目标方法需要一个key为user的POJO对象,该key还存在@SessionAttributes注解的value中,所以会抛出异常。如果此时session中存在key为user的值,则将该值传入目标方法参数。
怎么解决异常?
方法一:添加一个使用@ModelAttribute的注解的方法,该方法向implicitModel保存一个为key为user的POJO
方法二:去掉@SessionAttributes注解
方法三:修改目标参数的key值。例如 @ModelAttribute(value="user2") User user
4、若Handler没有标识@SessionAttributes注解或@SessionAttributes注解的value值中不包含key,则会通过反射来创建POJO类型的参数,并作为参数传入到目标方法
5、SpringMVC会把key和POJO类型的对象保存到implicitModel中,进而会保存到request中。
本文转自:http://www.cnblogs.com/caoyc/p/5636870.html
最新文章
- 小明的密码-初级DP解法
- BugTracker 加入发Mail的功能
- poj 3669 线段树成段更新+区间合并
- Oracle 数据库中不同事务并发访问的问题
- Qt Creator介绍
- Zabbix探索:资产信息的妙用
- BZOJ 1823 满汉全席
- 创建view
- 使用netty构建一个socks proxy
- VC中关于 0xcccccccc和 0xcdcdcdcd异常
- linux的学习系列 4---文件权限和访问模式
- 基于bootstrap的bootstrap-editable插件实现即时编辑功能
- swift MBProgressHUD加载gif或者apng的动图
- Linux平台 Oracle 12cR2 RAC安装Part3:DB安装
- 斐波那契数列第N项f(N)[矩阵快速幂]
- [转载] Thrift原理简析(JAVA)
- CTF---Web入门第二题 上传绕过
- Linux学习之CentOS(十九)------linux 下压缩与解压之 tar、gzip、bzip2、zip、rar
- Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)
- 字体转换网站——Font Squirrel