SPRINGMVC (ii)--process data model, Modelandview, model, MAP, redirect, @ModelAttribute,

Source: Internet
Author: User

1. Working with model data

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, Modelmap: entry for Org.springframework.ui.Model, Org.springframework.ui.   Modelmap or JAVA.UTI.MAP, when the processing method returns, the data in the MAP is automatically added to the model. Whether the return value is a string type or a Modelandview type, SPRINGMVC executes the handle method and resolves the return value to the Modelandview object. We put it in map or model, The data in the Modelmap is placed in the Modelandview object, used as Model – @SessionAttributes:: A property in the model is staged in httpsession so that the property can be shared between multiple requests – @Mod Elattribute: When the annotation is annotated with the method in the parameter, the object of the entry is placed in the data model.           The return value of the 2.ModelAndView controller processing method, if Modelandview, contains both view information and model data information. Add model data: –moelandview addobject (String attributename, Object attributevalue) –modelandview Addallobject (map<string,?> modelmap) settings view: –void Setview (View view)
–void setviewname (String viewName)
@RequestMapping (value= "/testmodelandview") public Modelandview Testmodelandview () {Modelandview mv = new Modelandview ( ); Mv.setviewname (SUCCESS);//logical view into physical view, look for the View resolver mv.addobject ("User", "Zhang San");//With Request.setattribute ("User", "Zhang San") function as return MV;}

3.Model, Map

Spring MVC internally uses a Org.springframework.ui.Model interface to store model data in concrete steps –spring MVC creates an implied model object as the storage capacity of the model data before invoking the method.     Manager – 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
@RequestMapping (value= "/testmap") public String TestMap (map<string,object> Map) {map.put ("user", New Student (" John Doe "," [email protected] "); return SUCCESS;} @RequestMapping (value= "/testmodelmap") public String Testmodelmap (Modelmap model) {Model.addattribute ("user", "hah") ; return SUCCESS;}

4. About redirection

In general, the Controller method returns a value of type string that is treated as a logical view name if the returned string is prefixed with forward: or redirect: As a prefix, SPRINGMVC treats them specially: forward: and redirect: as indicators , followed by a string as a URL to handle –redirect:/success.jsp: A redirect operation to Success.jsp is completed –forward:/success.jsp: A to success.jsp is completed The forwarding operation can be configured in the SPRINGMVC configuration file with the following code, our request can go directly to another page without the controller
<mvc:view-controller path= "/redirect" view-name= "Success"/>

But at this time the original access to the normal @requestmapping URL is not normal access, such as to normal access, plus the following code

<mvc:annotation-driven></mvc:annotation-driven>

[Email protected] [Usage Scenario]

Use @ModelAttribute annotations on method definitions: Spring MVC calls the method labeled @modelattribute on the method level one by one before invoking the target processing method. Use @ModelAttribute annotations before the method's entry: – You can get the object from the suppressed model data in the implied object, bind the request parameter to the object, and then pass in the parameter-add the method-in-parameter object to the model before each of us The method of seeking will be executed once
@ModelAttributepublic void Getstudent (map<string, object> Map) {Student Student = new Student ("Zhangsan", "[email Protected] "Map.put (" Stu ", Student);} @RequestMapping (value= "/toupdatepage", method=requestmethod.post) public String toupdatepage (@ModelAttribute (value = "Stu") Student Stu) {System.out.println (stu); Eturn "Success";}

6.SpringMVC determining the Target method Pojo type entry process

1. Determine a key:1). If the parameter of the target method's Pojo type does not use @modelattribute as a modifier and is also the default, the key is the lowercase of the first letter of the Pojo class name.        2). If you use the @modelattribute modifier, the key is the value of the @modelattribute annotation's values 2. In the Implicitmodel "request Domain", locate the object that corresponds to the key and, if present, pass in as the input parameter.      If the map is saved in the @modelattribute tag method, and key is the same as the key identified in 1, it will be obtained! 3. There is no key corresponding object in Implicitmodel, check whether the current handler uses @sessionattribute annotation decoration, if the annotation is used,      And the value of the @sessionattributes annotation contains key, it will get the value of key from HttpSession, and if it exists, it will be passed directly to the input parameter of the target method, and if not present, an exception is thrown. 4. If handler does not identify the @sessionattributes annotation or the value of the @sessionattributes annotation does not contain a key, thereflection "must have a parameterless constructor" to create a parameter of type POJO, passed in as a parameter of the target method. 5.SpringMVC saves the key and the POJO object to the Implicitmodel, which in turn is saved to the request domain!
Note: The [email protected] Tagged method is called by SPRINGMVC before each target method executes! [Email protected] annotations can be used to decorate the POJO type of the target method, whose Value property values have the following effect: 1) SPRINGMVC uses the Value property values to find the corresponding object in Implicitmodel, if The presence is passed directly into the parameter of the target Method 2) SPRINGMVC will be the value of the key, the object of the POJO type is value, deposited into the request domain. 7. If you want to share a model property data across multiple requests, you can label a @sessionattribute,springmvc on the controller class to HttpSession the corresponding property in the model-only on the class, Allows multiple requests to share a subset of data values: Specifies the key types in the session field: Specifies the byte code of the object to be placed in the session field the following code implements the modified Stu in the session, which can be invoked on the page through an EL expression

Can be implemented with the types attribute, but generally not, trouble prone

Springmvc (ii)--process data model, Modelandview, model, MAP, redirect, @ModelAttribute,

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.