Spring's @modelattribute annotations

Source: Internet
Author: User

1.

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

2.

@ModelAttribute the use of detailed

[Email protected] annotation method
Example (1), (2), (3) Similar, the method that is annotated by @modelattribute is executed before each method of the controller is executed, so use it sparingly for the use of a controller to map multiple URLs.

(1) @ModelAttribute comment void return value method

[Java]View PlainCopyprint?
  1. <span style="FONT-SIZE:12PX;" >@Controller
  2. public class Helloworldcontroller {
  3. @ModelAttribute
  4. public void Populatemodel (@RequestParam String ABC, model model) {
  5. Model.addattribute ("AttributeName", ABC);
  6. }
  7. @RequestMapping (value = "/helloworld")
  8. Public String HelloWorld () {
  9. return "HelloWorld";
  10. }
  11. }</span>
  12. <span style="FONT-SIZE:12PX;" >
  13. </span>

In this example, after obtaining the request/helloworld, the Populatemodel method is called before the HelloWorld method, which puts the request parameter (/helloworld?abc= Text) is added to a model property named AttributeName, and HelloWorld is called after it executes, returning the view name HelloWorld and model have been produced by the @modelattribute method.
In this example, the model property name and the Model Property object have a model.addattribute () implementation, but only if you want to include a parameter of model type in the method.
When the URL or post does not contain the secondary parameters, will be error, in fact, do not need this method, you can completely write the method of the request, so that the lack of this parameter is not error

[Java]View PlainCopyprint?
    1. <span style= "FONT-SIZE:12PX;" >        @RequestMapping (Value =  "/helloworld")   
    2.          public string helloworld (STRING&NBSP;ABC)  {  
    3.             return  "HelloWorld";   
    4.         }</span>  
    5. <span  style= "FONT-SIZE:12PX;" >  
    6. </span>  


(2) How to return a specific class @ModelAttribute comment

[Java]View PlainCopyprint?
    1. <span style="FONT-SIZE:12PX;" >@ModelAttribute
    2. Public account AddAccount (@RequestParam String number) {
    3. return Accountmanager.findaccount (number);
    4. }
    5. </span>
    6. <span style="FONT-SIZE:12PX;" >
    7. </span>

In this case, the name of the model property is not specified, it is implicitly represented by the return type, and if this method returns the account type, the name of the model property is account.
In this example, the Model property name has an implied representation of the return object type, and the Model Property object is the return value of the method. It does not need to have a specific parameter.

(3) @ModelAttribute (value= "") Comment returns the method of the specific class

[Java]View PlainCopyprint?
  1. <span style="FONT-SIZE:12PX;" >@Controller
  2. public class Helloworldcontroller {
  3. @ModelAttribute ("AttributeName")
  4. Public string AddAccount (@RequestParam string abc) {
  5. return ABC;
  6. }
  7. @RequestMapping (value = "/helloworld")
  8. Public String HelloWorld () {
  9. return "HelloWorld";
  10. }
  11. }</span>
  12. <span style="FONT-SIZE:12PX;" >
  13. </span>


In this example, the Value property of the @modelattribute annotation is used to specify the name of the model property. The Model Property object is the return value of the method. It does not need to have a specific parameter.

(4) @ModelAttribute and @requestmapping simultaneously annotate a method

[Java]View PlainCopyprint?
  1. <span style="FONT-SIZE:12PX;" >@Controller
  2. public class Helloworldcontroller {
  3. @RequestMapping (value = "/helloworld.do")
  4. @ModelAttribute ("AttributeName")
  5. Public String HelloWorld () {
  6. return "HI";
  7. }
  8. }</span>
  9. <span style="FONT-SIZE:12PX;" >
  10. </span>


The return value of this method does not represent a view name, but rather the value of the Model property, and the view name is converted from Requesttoviewnametranslator to logical view HelloWorld according to the request "/helloworld.do".
The Model property name has @modelattribute (value= "") specified, which is equivalent to encapsulating the Key=attributename,value=hi in the request.

[Email protected] Note the parameters of a method

(1) Get from model

[Java]View PlainCopyprint?
  1. <span style="FONT-SIZE:12PX;" >@Controller
  2. public class Helloworldcontroller {
  3. @ModelAttribute ("user")
  4. Public User AddAccount () {
  5. return new User ("JZ","123");
  6. }
  7. @RequestMapping (value = "/helloworld")
  8. Public String HelloWorld (@ModelAttribute ("user") user user) {
  9. User.setusername ("Jizhou");
  10. return "HelloWorld";
  11. }
  12. }</span>
  13. <span style="FONT-SIZE:12PX;" >
  14. </span>


In this example, the @ModelAttribute ("user") User user annotation method parameter, the value of the parameter user is derived from the Model property in the AddAccount () method.
If the method body is not labeled @sessionattributes ("User"), then scope is request and if marked, then scope is session

(2) Get from form form or URL parameter (in fact, you can get the user object without making this comment)

[Java]View PlainCopyprint?
  1. <span style="FONT-SIZE:12PX;" >@Controller
  2. public class Helloworldcontroller {
  3. @RequestMapping (value = "/helloworld")
  4. Public String HelloWorld (@ModelAttribute User user) {
  5. return "HelloWorld";
  6. }
  7. }</span>
  8. <span style="FONT-SIZE:12PX;" >
  9. </span>

Spring's @modelattribute annotations

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.