您的位置:首页 > 编程语言 > Java开发

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这个,所以反射会自己创建,那么我们就跟他匹配一个相同的,然后让他注入进去
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: