SPRINGMVC Processing Model data

Source: Internet
Author: User

1. The so-called processing model data: The object returned in the action method, how the collection is placed into the domain object. 1). Domain object: PageContext, request, session, application
2. Spring MVC provides the following ways to output model data:
1) Modelandview: Processing method when the return value type is Modelandview, the method body can add model data through the object @RequestMapping("/testmodelandview" )Public Modelandview Testmodelandview () { modelandview modelandview = new modelandview ( SUCCESS );           //Add model data to Modelandview Modelandview.addobject ( "Time", new Date ());           return Modelandview;     }
2). Map and Model: When the entry is Org.springframework.ui.Model, Org.springframework.ui.ModelMap, or JAVA.UTI.MAP, when the processing method returns, the map Data is automatically added to the model. ----------------------------------------------------------------------------------------------------------- --------------@RequestMapping ( "/testmap" ) Public String testmap ( map<string,object> Map) {System. Out. println (Map.getclass (). GetName ()); Map.put ( "Time", new Date ()); return SUCCESS ;     }==================================================================/*** Steps:* 1. Remove the corresponding record from the database: [Id=1, USERNAME=AA, [email protected], age=11]* 2. SPRINGMVC assigns a value to its corresponding property: [Id=1, USERNAME=AA, [email protected], age=13]* 3. Page Transfer value execution modification: update: [Id=1, USERNAME=AA, [email protected], age=13]      */ @RequestMapping( "/testmodelattribute")Public String testmodelattribute ( @ModelAttribute("user" ) user user) {System. Out. println ("UPDATE:" + user); return SUCCESS ;     }····················································································································· ····························/*** Can simulate STRUTS2 's Prepare interceptor!      */ @ModelAttribute Public void getUser (@RequestParam (value="id" , required= false ) Integer ID,map<string, object> Map) { if(id! = null ) {User user = Userdao. GetUser (ID);System. Out. println (user); map.put ( "user", user);System. Out. println ("GetUser" );           }     }
explanation: Using the @ModelAttribute () annotation in spring MVC, mark the note to a method with a return value: Spring MVC calls the method at the method level, one by another, before invoking the target processing method. , and add the return values of these methods to the model. ==========================================================================
★ @SessionAttributes:staging a property in the model into HttpSession so that the property can be shared between multiple requests
Example: @SessionAttributes(value={"username " ,"age" ,"user " },types={string. class })
Use @SessionAttributes:
A. @SessionAttributes annotations can only be marked on the top of the class, not on the method. B. Function: If the model data is stored in the request in a method, if the @SessionAttributes () annotation is used on the class and the value contains key, then Springmvc c7> will also put the model data into the session.

@SessionAttributes In addition to specifying the properties that need to be placed into the session through the property name, you can also specify which model properties need to be placed into the session by the object type of the Model property @sessionattributes (types= User.class) Adds all attributes of type User.class in the implied model to the session.
@SessionAttributes (value={"user1", "User2"}) @SessionAttributes (Types={user.class, Dept.class}) @SessionAttributes (value={"user1", "User2"}, Types={dept.class})

an anomaly about @SessionAttributes flowerorg.springframework.web.HttpSessionRequiredException:Session attribute ' user ' required-not found in Session
Analysis:

1-If the form is submitted, the entry for the target method is the bean corresponding to the form, then SPRINGMVC automatically places the bean corresponding to the request, and the key: class name first letter lowercase, value: bean instance

@RequestMapping ("/testsessionattributes3")
Public String testSessionAttributes3 (user user) {
SYSTEM.OUT.PRINTLN (user);
Return "Success";
}

Equivalent:

@RequestMapping ("/testsessionattributes3")
Public String testSessionAttributes3 (user user, map<string, object> Map) {
SYSTEM.OUT.PRINTLN (user);
Map.put ("user", user);
Return "Success";
}

Or

@RequestMapping ("/testsessionattributes3")
Public String TestSessionAttributes3 (@ModelAttribute ("user") user user) {
SYSTEM.OUT.PRINTLN (user);
Return "Success";
}

2-If a @SessionAttributes annotation is added to the class on which the target method is located, and the key and the target method @ModelAttribute the same key, before the target method is called, SPRINGMVC will first
The previous session gets the attribute value and assigns the attribute value to the parameter, and throws the above exception if there is no corresponding attribute in the session.

3-FIX: Make the key of the @ModelAttribute different from the key of the @SessionAttributes; before calling the target method, make a corresponding attribute in the Session, define a new method, add @ModelAttribute annotation before the method, and the key is Key for @SessionAttribute ()

@SessionAttributes (value={" User"})
@Controller
public class Cotrollertest {

@ModelAttribute (" User")
Public User GetUser () {
return new User ();
}

@RequestMapping ("/testsessionattributes3")
Public String TestSessionAttributes3 (@ModelAttribute (" User") (user user) {
SYSTEM.OUT.PRINTLN (user);
Return "Success";
}

}

Analyze Execution Order:Call the GetUser method, return the user, the value of the annotation to the @ModelAttribute () annotation of the method is user, and the value of the @SessionAttributes () above the class also has user, then put the return value into the session! , call the target method, notice that the input parameter has a @ModelAttribute ("user") adornment, and that the user and the class's @SessionAttributes () match, the model data is fetched from Sessioin and then passed into the target method.

SPRINGMVC Processing Model data

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.