SPRINGMVC @ModelAttribute Study
Blog Category:
@ModelAttribute binding request parameters to a Command object
@ModelAttribute one has the following three functions:
① bind request parameter to command object: When placed on the parameter of a function processing method, it is used to bind multiple request parameters to a command object, thus simplifying the binding
and automatically exposed to model data for use in view page presentations;
② exposing a form reference object to model data: when placed on a processor's general method (non-functional processing method), prepares the form reference to be presented for the form
object, such as the city in which you want to select when registering, and automatically add a function before you perform a functional processing method (@RequestMapping annotation)
To the model object for use when the view page is displayed;
③ Exposure @requestmapping method returns a value of model data: when placed on the return value of a function processing method, the return value of the exposed function processing method is
Model data for use when the view page is displayed.
binding request parameters to a specified object
Java code
- Public String test1 (@ModelAttribute ("user") Usermodel user)
It's just that there's one more annotation @modelattribute ("user"), whose purpose is to add the bound command object to the model object with a name of "user" for use in the View page display. We can now use ${user.username} on the view page to get the properties of the bound Command object.
If the request parameter contains "? USERNAME=ZHANG&PASSWORD=123&WORKINFO.CITY=BJ", it is automatically bound to the city property of the Workinfo property in user.
Java code
- @RequestMapping (value="/model2/{username}")
- Public String test2 (@ModelAttribute ("model") Databindertestmodel model)
URI template variables can also be automatically bound to the Command object when you request a URL that contains "bool=yes&schooinfo.specialty=computer&hobbylist[0]=program& Hobbylist[1]=music&map[key1]=value1&map[key2]=value2&state=blocked "is automatically bound to the Command object. The URI template variable has a high priority when the URI template variable and the request parameter have the same name.
second, exposing the form reference object as model data
Java code
- /**
- * After setting this annotation you can use the HB object (List) collection directly on the front page
- * @return
- */
- @ModelAttribute ("HB")
- Public list<string> hobbieslist () {
- list<string> hobbise = new linkedlist<string> ();
- Hobbise.add ("basketball");
- Hobbise.add ("Football");
- Hobbise.add ("tennis");
- return hobbise;
- }
JSP pages show up
Java code
- <br>
- Initialized data: ${HB}
- <br>
- <c:foreach items="${HB}" var="hobby" varstatus="vs" >
- <c:choose>
- <c:when test="${hobby = = ' basketball '}" >
- Basketball <input type="checkbox" Name="Hobbies" value="basketball" >
- </c:when>
- <c:when test="${hobby = = ' Football '}" >
- Football <input type="checkbox" Name="Hobbies" value="Football" >
- </c:when>
- <c:when test="${hobby = = ' tennis '}" >
- Tennis <input type="checkbox" Name="Hobbies" value= "Tennis" >
- </c:when>
- </c:choose>
- </c:forEach>
Note:
1. The contents of a set can be displayed in this way
2, the above JSP code is using JSTL, need to import JSTL related jar package
<% @taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
Iii. Exposure @requestmapping method return value is model data
Java code
- Public @ModelAttribute ("user2") Usermodel test3 (@ModelAttribute ("user2") Usermodel user)
You can see that the return value type is a command object type, and through @modelattribute ("user2") annotations, the return value is exposed to the model data (named User2) for use in the view display
The return value of the @ModelAttribute annotation overrides the command object with the same name as the @modelattribute annotation in the @requestmapping annotation method
SPRINGMVC @ModelAttribute Study