SPRINGMVC @ModelAttribute Study

Source: Internet
Author: User

SPRINGMVC @ModelAttribute Study Blog Category:
    • Spring

@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
    1. 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
    1. @RequestMapping (value="/model2/{username}")
    2. 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
  1. /**
  2. * After setting this annotation you can use the HB object (List) collection directly on the front page
  3. * @return
  4. */
  5. @ModelAttribute ("HB")
  6. Public list<string> hobbieslist () {
  7. list<string> hobbise = new linkedlist<string> ();
  8. Hobbise.add ("basketball");
  9. Hobbise.add ("Football");
  10. Hobbise.add ("tennis");
  11. return hobbise;
  12. }

JSP pages show up

Java code
  1. <br>
  2. Initialized data: ${HB}
  3. <br>
  4. <c:foreach items="${HB}" var="hobby" varstatus="vs" >
  5. <c:choose>
  6. <c:when test="${hobby = = ' basketball '}" >
  7. Basketball <input type="checkbox" Name="Hobbies" value="basketball" >
  8. </c:when>
  9. <c:when test="${hobby = = ' Football '}" >
  10. Football <input type="checkbox" Name="Hobbies" value="Football" >
  11. </c:when>
  12. <c:when test="${hobby = = ' tennis '}" >
  13. Tennis <input type="checkbox" Name="Hobbies" value= "Tennis" >
  14. </c:when>
  15. </c:choose>
  16. </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
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.