@ModelAttribute can be used to modify the methods and parameters in the controller, binding the value of the decorated object to the attribute of the specified name.
When the method is decorated, the value returned by the method is bound one time before each access process in the controller.
When modifying a parameter, if you want to redirect or forward to another method, you can also get the parameter value in the model in another method, you can use two methods (suppose that method a jumps to method B, to get the parameters in method a):
1. Place the parameters in method A in the redirectattributes , use the redirect input to the B method, and get the parameters in method B.
2. Use forward to forward to B and use the httpservletrequest. getattribute () method to get the parameters.
Test code:
1 @Controller2@RequestMapping ("/")3 Public classModeltest {4@Value ("WJ")5 PrivateString UserName;6 7@ModelAttribute ("SayHello")8 PublicString runbeforeeachhandlercalled () {9System.out.println ("runbeforeeachhandlercalled Method Execution");TenString SayHello = "Hello" +UserName; One returnSayHello; A } - -@RequestMapping (value = "/Home", method =requestmethod.get) the PublicString tohomepage () { - return"Index"; - } - +@RequestMapping (value = "/handler1") - PublicString Redirectmethod (@ModelAttribute ("Key2") String Key2, Redirectattributes attrbs) { +System.out.println ("Handler1"); A atKey2 = "Key2value"; - -Attrbs.addattribute ("RedirectAttr1", "Redirectattr1value"); -Attrbs.addflashattribute ("RedirectAttr2", "Redirectattr2value"); - - //return "Forward:/handler2"; in //return new Redirectview ("Handler2"); - return"Redirect:handler2"; to } + -@RequestMapping (value = "/handler2") the Publicstring redirectdestination (Modelmap map, string redirectAttr1, *@ModelAttribute ("RedirectAttr2") String redirectAttr2, $ httpservletrequest request, Redirectattributes Attrbs) {Panax NotoginsengSystem.out.println ("Handler2"); -System.out.println ("Modelmap.key2 value is:" + map.get ("Key2")); theSystem.out.println ("Redirect.request.key2:" + request.getattribute ("Key2")); +System.out.println ("REDIRECT.REQUEST.REDIRECTATTR1:" + request.getattribute ("RedirectAttr1")); ASystem.out.println ("RedirectAttr1 value is:" +redirectAttr1); theSystem.out.println ("RedirectAttr2 value is:" +redirectAttr2); +Map.put ("Key3", "Key3value"); -Attrbs.addattribute ("Key4", "Key4value"); $ return"Forward:handler3"; $ } - -@RequestMapping (value = "/handler3") the PublicString forwarddestination (Modelmap map, @ModelAttribute ("RedirectAttr1") String redirectAttr1, -@ModelAttribute ("RedirectAttr2") String redirectAttr2, @ModelAttribute ("Key4") String Key4,Wuyi HttpServletRequest Request) { theSystem.out.println ("Handler3"); -System.out.println ("Modelmap.key3 value is:" + map.get ("Key3")); WuSystem.out.println ("Forward.request.key3:" + request.getattribute ("Key3")); -System.out.println ("FORWARD.REQUEST.REDIRECTATTR1:" + request.getattribute ("RedirectAttr1")); AboutSystem.out.println ("RedirectAttr1 value is:" +redirectAttr1); $System.out.println ("RedirectAttr2 value is:" +redirectAttr2); -System.out.println ("Key4 value is:" +key4); - return"Index"; - } A +}
View Code
INDEX.JSP:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><HTML><Body><H2>${sayhello}</H2></Body></HTML>
View Code
Output:
the Runbeforeeachhandlercalled method executes the Modelmap.key2 value in the Handler1 runbeforeeachhandlercalled method execution Handler2: NULL Redirect.request.key2: NULL REDIRECT.REQUEST.REDIRECTATTR1: NULL The redirectAttr1 value is: Redirectattr1valueredirectattr2 value: Redirectattr2valuerunbeforeeachhandlercalled method Execution Handler3 MODELMAP.K Ey3 value:nullforward.request.key3:key3valueforward.request.redirectAttr1:null The REDIRECTATTR1 value is: The redirectattr1valueredirectattr2 value is: Redirectattr2valuekey4 value :
View Code
Modelattribute annotations are used with spring redirection parameters