Spring MVC provides several ways to output the model Data:
Modelandview: the method body can add model data through the object when the processing method returns a value of type Modelandview
Map and Model: when the processing method returns, when the entry is org.springframework.ui.Model, org.springframework.ui.ModelMap, or java.uti.Map, the map Data is automatically added to the Model.
@SessionAttributes: a property in the model is staged in httpsession so that the property can be shared between multiple requests
@ModelAttribute: when the annotation is annotated with a method in the parameter, the object of the entry is placed in the data model
1.ModelAndView
The return value of the controller processing method, if modelandview, contains both the view information and the model data Information.
To add Model Data:
Moelandview addobject (String attributename, Object Attributevalue)
Modelandview Addallobject (map<string,?> Modelmap)
To set the View:
void Setview (view View)
void Setviewname (String ViewName)
Example:
JSP Code:
1 <a href= "springmvc/testmodelandview" >testModelAndView</a><br>
View Code
Java
1 @RequestMapping (value= "/testmodelandview")2public Modelandview Testmodelandview () {3 String viewname=SUCCESS; 4 Modelandview model=New Modelandview (viewName); 5 New Date ()); 6 return model; 7 }
View Code
Where ViewName is the name of the returned view, that is, the page address on which page the data is Displayed.
The model object can be thought of as a container for storing data, which can be used to add objects of Key-value pairs, and then remove them from the displayed Page.
1 time : 2 ${requestscope.time}
View Code
Mav can be understood as M to v data, SPRINGMVC the model data into the request Domain.
2. Map and Model
Spring MVC internally uses a Org.springframework.ui.Model interface to store model data
Specific steps:
Spring MVC creates an implied model object as a storage container for model data before invoking the Method. If the Method's incoming parameter is a MAP or model type, Spring MVC passes a reference to the implied model to those Arguments. In the body of the method, the developer can access all the data in the model through this entry object, or you can add new attribute data to the model
Jsp
1 <a href= "springmvc/testmap" >testMap</a><br>
View Code
Java
1 /*2 * Target method can add parameters of map type3 * Actual can also be model or Modelmap type parameter4 * Org.springframework.validation.support.BindingAwareModelMap5 */6@RequestMapping (value= "/testmap")7 publicString TestMap (map<string,object>Map) {8 System.out.println (map.getclass (). getName ());9Map.put ("names", arrays.aslist ("harry", "zhang san", "john doe"));Ten one returnSUCCESS; a}
View Code
success.jsp
[email protected]
If you want to share a model property data across multiple requests, You can label a @SessionAttributes on the controller class, and Spring MVC will temporarily save the corresponding properties in the model to HttpSession.
@SessionAttributes you can specify which model properties need to be placed in the session by the object type of the model property, except that you can specify the properties that need to be placed in the session through the property name
@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})
Example
Jsp
1 <a href= "springmvc/testsession" >testSession</a><br>
View Code
Java
1 @RequestMapping (value= "/testsession")2public String Testsessionattribute (map<string,object> Map) {3 user user=new user ("baipeng", "123", "[email protected]", at a); 4 Map.put ("user", user); 5 Map.put ("school", "cumt"); 6 return SUCCESS; 7 }
View Code
Results
Both the request and the session domain can be obtained.
SPRINGMVC Learning (v.)--processing Model data