spring mvc ModelAttribute 会在目标方法执行前,执行
2015-01-21 17:05
393 查看
modelAttribute 会在目标方法执行前,执行
package com.ao.handler;
import java.util.Map;
import javax.validation.Valid;
import javax.ws.rs.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.ao.dao.DepartmentDao;
import com.ao.dao.EmployeeDao;
import com.ao.entities.Employee;
@Controller
public class EmployeeHandler {
@Autowired
private DepartmentDao departmentDao;
@Autowired
private EmployeeDao employeeDao;
@ModelAttribute
public void getUser(@RequestParam(value="id",required=false) Integer id,
Map<String,Object> map){
if(id!=null){
User user = new User(1,"Tom","123456","qwe@",12);
System.out.println("重数据库中获取的对象"+user);
map.put("user", user);
}
}
@RequestMapping("/testModelAttribute")
public String testModelAttribute(User user){
System.out.println("修改:"+user);
return SUCCESS;
}
}
经过map后 spring mvc 又在下面进行匹对user
如果 map.put("abc",user)
那么在下面要改成 @ModelAttribute("abc") User user
对于下面的没有abc这个,所以反射会自己创建,那么我们就跟他匹配一个相同的,然后让他注入进去
package com.ao.handler;
import java.util.Map;
import javax.validation.Valid;
import javax.ws.rs.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.ao.dao.DepartmentDao;
import com.ao.dao.EmployeeDao;
import com.ao.entities.Employee;
@Controller
public class EmployeeHandler {
@Autowired
private DepartmentDao departmentDao;
@Autowired
private EmployeeDao employeeDao;
@ModelAttribute
public void getUser(@RequestParam(value="id",required=false) Integer id,
Map<String,Object> map){
if(id!=null){
User user = new User(1,"Tom","123456","qwe@",12);
System.out.println("重数据库中获取的对象"+user);
map.put("user", user);
}
}
@RequestMapping("/testModelAttribute")
public String testModelAttribute(User user){
System.out.println("修改:"+user);
return SUCCESS;
}
}
经过map后 spring mvc 又在下面进行匹对user
如果 map.put("abc",user)
那么在下面要改成 @ModelAttribute("abc") User user
对于下面的没有abc这个,所以反射会自己创建,那么我们就跟他匹配一个相同的,然后让他注入进去
相关文章推荐
- Spring事务管理(6)-执行目标方法
- 获得spring的指定目标对象,执行指定方法(JDK动态代理,cglib动态代理,Dubbo-Javassist代理)
- Spring aop 注解方式怎么获得执行了目标的某个方法?
- springMvc 目标方法执行前改变参数(环绕增强)
- Spring aop 注解方式怎么获得执行了目标的某个方法?
- 详解对密码执行散列和 salt 运算方法
- 在Window2003上执行System.Diagnostics.Process.GetProcessesByName等方法失败的原因
- 得出SQL语句的执行时间的方法
- 定时执行任务的三种方法
- 反射时用SetValue方法,报“不能从目标类型扩展到基元类型”
- SqlCommand的ExecuteNonQuery()方法执行多条T-SQL语句
- Session_End()这个方法,为什么在会话结束的时候没有执行?
- 9x下通过执行文件名获得进程ID的方法
- 得出SQL语句的执行时间的方法
- 该页无法显示 您试图从目录中执行 CGI、ISAPI 或其他可执行程序,但该目录不允许执行程序。 解决方法
- 跟踪 session 的执行,分析跟踪日志文件,解决问题的好方法
- 防止.net代码二次执行方法2则(VB.net)
- 怎样用API执行可执行文件(三种方法)
- Visual Studio 2005安装后,原来的Asp.net1.1不能执行的解决方法。
- 实现性能目标的几种方法