SPRINGMVC processing model data (that is, data queried from the database is placed in the request domain)

Source: Internet
Author: User

This is about the data that is queried from the database and stored in the request domain. The value can then be obtained directly from the request domain on the page.

There are 4 ways of doing this:

1): Modelandview is used as an object.

1 /**2 * The return value of the target method can be a modelandview type. 3 * It can contain view and model information4 * Springmvc will put the data in the Modelandview model into the request domain object.5      * @return6      */7@RequestMapping ("/testmodelandview")8      PublicModelandview Testmodelandview () {9String ViewName =SUCCESS;TenModelandview Modelandview =NewModelandview (viewName); One          A         //add model data to Modelandview. -Modelandview.addobject ("Time",NewDate ()); -          the         returnModelandview; -}

Get from the page:

Time: ${requestscope.time}

Both 2:model and map are passed in as parameters.

1 @RequestMapping ("/testmap")2public      String testmap (map<string, Object > map) {3        4         map.put ("Names", Arrays.aslist ("Tom", "Jerry", "Mike" )); 5         return SUCCESS; 6     }

3: @ModelAttribute

/**
* 1. There are @ModelAttribute tagged methods that are called by SPRINGMVC before each target method executes!
* 2. @ModelAttribute annotations can also be used to modify the entry parameters of the POJO type of the target method, whose Value property has the following effect:
* 1). SPRINGMVC uses the Value property values to find the corresponding object in the Implicitmodel, and if it exists, it is passed directly into the parameter of the target method.
* 2). SPRINGMVC will be a value of key, the object of the POJO type is value, which is deposited into the request.
*/
@ModelAttribute
public void GetUser (@RequestParam (value= "id", required=false) Integer ID,
Map<string, object> map) {
System.out.println ("Modelattribute method");
if (id! = NULL) {
To simulate getting an object from the database
User user = new User (1, "Tom", "123456", "[email protected]", 12);
System.out.println ("Get an object from the database:" + user);

Map.put ("user", user);
}
}

SPRINGMVC processing model data (that is, data queried from the database is placed in the request domain)

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.