@ModelAttribute可以用于修饰controller里的方法和参数,将被修饰的对象的值绑定到指定名称的属性里。
当修饰方法时,方法返回的值会在该controller里每个访问处理前绑定一次。
修饰参数时,如果想重定向或者转发到另一个方法后,还能在另一个方法中取得model里的参数值,可以使用两种方法(假设方法A里跳到方法B,要取得方法A里的参数):
1.将方法A里的参数放在RedirectAttributes里,使用重定向进到B方法,在方法B里获取参数。
2.使用forward转发到B,使用HttpServletRequest.getAttribute()方法获取参数。
测试代码:
1 @Controller 2 @RequestMapping("/") 3 public class ModelTest { 4 @Value("wj") 5 private String userName; 6 7 @ModelAttribute("sayHello") 8 public String runBeforeEachHandlerCalled() { 9 System.out.println("runBeforeEachHandlerCalled方法执行"); 10 String sayHello = "Hello " + userName; 11 return sayHello; 12 } 13 14 @RequestMapping(value = "/home",method = RequestMethod.GET) 15 public String toHomePage() { 16 return "index"; 17 } 18 19 @RequestMapping(value = "/handler1") 20 public String redirectMethod(@ModelAttribute("key2")String key2, RedirectAttributes attrbs) { 21 System.out.println("handler1里"); 22 23 key2 = "key2value"; 24 25 attrbs.addAttribute("redirectAttr1","redirectAttr1value"); 26 attrbs.addFlashAttribute("redirectAttr2","redirectAttr2value"); 27 28 // return "forward:/handler2"; 29 // return new RedirectView("handler2"); 30 return "redirect:handler2"; 31 } 32 33 @RequestMapping(value = "/handler2") 34 public String redirectDestination(ModelMap map, String redirectAttr1, 35 @ModelAttribute("redirectAttr2")String redirectAttr2, 36 HttpServletRequest request, RedirectAttributes attrbs) { 37 System.out.println("handler2里"); 38 System.out.println("ModelMap.key2值为:" + map.get("key2")); 39 System.out.println("redirect.request.key2:" + request.getAttribute("key2")); 40 System.out.println("redirect.request.redirectAttr1:" + request.getAttribute("redirectAttr1")); 41 System.out.println("redirectAttr1值为:" + redirectAttr1); 42 System.out.println("redirectAttr2值为:" + redirectAttr2); 43 map.put("key3","key3value"); 44 attrbs.addAttribute("key4","key4value"); 45 return "forward:handler3"; 46 } 47 48 @RequestMapping(value = "/handler3") 49 public String forwardDestination(ModelMap map,@ModelAttribute("redirectAttr1") String redirectAttr1, 50 @ModelAttribute("redirectAttr2")String redirectAttr2,@ModelAttribute("key4")String key4, 51 HttpServletRequest request) { 52 System.out.println("handler3里"); 53 System.out.println("ModelMap.key3值为:" + map.get("key3")); 54 System.out.println("forward.request.key3:" + request.getAttribute("key3")); 55 System.out.println("forward.request.redirectAttr1:" + request.getAttribute("redirectAttr1")); 56 System.out.println("redirectAttr1值为:" + redirectAttr1); 57 System.out.println("redirectAttr2值为:" + redirectAttr2); 58 System.out.println("key4值为:" + key4); 59 return "index"; 60 } 61 62 }
index.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <body> <h2>${sayHello}</h2> </body> </html>
输出:
runBeforeEachHandlerCalled方法执行 handler1里 runBeforeEachHandlerCalled方法执行 handler2里 ModelMap.key2值为:null redirect.request.key2:null redirect.request.redirectAttr1:null redirectAttr1值为:redirectAttr1value redirectAttr2值为:redirectAttr2value runBeforeEachHandlerCalled方法执行 handler3里 ModelMap.key3值为:null forward.request.key3:key3value forward.request.redirectAttr1:null redirectAttr1值为:redirectAttr1value redirectAttr2值为:redirectAttr2value key4值为: