SPRINGMVC Process Model data

Source: Internet
Author: User
Tags lowercase modifier

MVC design Pattern: Sending requests to the target processor, the target processor is to tune the business method, the method may return a value (object or set), and then forward to the page, need to return the method object or combine the reality to the page, there is a problem (how to put the model data into a domain object, the domain object usually refers to the request domain)

Four ways to solve this problem:

One, Modelandview: Processing method return value type is Modelandview, the method body can add model data through that object

The return value of the target method can be the Modelandview type, which can contain view and model information; SPRINGMVC will put the model data in Modelandview into the request field.

Ctrl + shift+f

Second, map and Model: Enter the reference for Org.springframework.ui.Model, Org.springframework.ui.ModelMap or JAVA.UTI.MAP, the data in the map is automatically added to the model when the processing method returns.

The target method can add a parameter to a map type (which can actually be either a model type or a modelmap type)

Put it in the request domain.

Third, @SessionAttributes; temporarily saves a property in the model to HttpSession, which can be shared between multiple requests.

If you want a model property data to be common between multiple requests, you can mark a @sessionattributes,springmvc on the controller class to temporarily save the corresponding properties in the model to httpsession.

This is written in the request domain:

To put in session, you need to add a @sessionattributes annotation to the top of the controller: This also places the attribute in the map both in the request domain and in the session field.

You can put multiple arguments, types={string.class} if a string exists in the map, it is automatically placed in the session field

You can also specify which module properties need to be placed in the session (actually using the types property value) by specifying the object type of the Model property, in addition to the attributes that you want to place in the session by using the property name (in effect, the value attribute).

↑ Note: This annotation can only be placed above the class and cannot be decorated with methods.

Four, @ModelAttribute: After the method has entered the annotation, the object in the parameter is placed in the data model.

Usage Scenario: A field cannot be modified, the form

There is a problem with hidden fields (1 if the sensitive field "password" 2 modifies the field more)

Please attach to him when you update it-more trouble

Get directly from the database, rather than a new object:

Sample code:

* * 1. A method with a @modelattribute tag executes when each target method is springmvc called

2. @ModelAttribute annotations can also modify the Pojo type of the target method, and its Value property values act as follows:

2.1SpringMVC finds the corresponding object in the Implicitmodel using the Value property values, and if it exists, it is passed directly into the target method's entry.

2.2SpringMVC is-value to the Key,pojo type object as value and stored in the request

Note @sessionattributes throws an exception because the following method is not written:

3. If there is no key object in the Implicitmodel, check to see if the current handler uses the @sessionattributes annotation modifier, and if the annotation is used, The value attribute of the @sessionattributes annotation contains key, then the value of the key should be obtained from the httpsession, and if there is a direct incoming parameter of the target method, the exception will be thrown if it does not exist.

Solution: Must add @modelattribute

Springmvc

Operation Principle:

Run the process:

1. Perform the @modelattribute annotation cosmetic method: From the database to place objects, the object into the map, the key is: User

2.SpringMVC takes the user object out of the map and assigns the form's request parameter to the corresponding property of the user object->spring to the completed

3.SpringMVC to pass the above object to the parameters of the target method

Note: In @ModelAttribute decorated method, the key needed to be placed in the map is the same as the first letter lowercase string of the target method entry type

SOURCE Analysis:

1. First of all, the @modelattribute annotation modification method actually puts the data in the map of the @modelattribute method into the Implicitmodel

2. Resolves the target parameter of the request processor, which actually comes from the target attribute of the Webdatabinder object

1) Create Webdatabinder Object

① determines objectnamed property: If the Attrnamez property value passed is "", ObjectName is the first letter of the class name lowercase

* Note: attrname if the Pojo property of the target method is decorated with @modelattribute, then the Attrname value is the value of @modelattribute value

② Determine the target attribute

> Determine Implicitmodel to find the corresponding properties of arrtname, if there is OK * if not present: Then verify that the current handler is using @sessionattributes to decorate, if used, An attempt is made to get the property value of the Attrname from the session. If there is no corresponding property value in the session, an exception is thrown. Handler is not decorated with @sessinattributes, or the key and attrname specified by the value in Sessionattributes are not matched, the object is created through Pojo.

2) Springmvc the form's request parameters to the Webdatabinder target property

3) *SPRINGMVC will webdatabinder Attrname and target to Implicitmodel, and then conduction the request domain object. (to be mentioned later)

4) Webdatabinder target is passed as a parameter to the entry of the target method

When changing values, the Set method home breakpoint

Conclusion: SPRINGMVC determines the process by which the target method Pojo the type of the parameter:

1. Determine a key

1 if the Pojo type of the target method does not use @modelattribute as the modifier, then the key is the lowercase of the first letter of the Pojo class name (

2 If the @modelattribute is used to decorate, the key is the value attribute of the @modelattribute annotation.

2. Find the corresponding key corresponding in the Implicitmodel, and if it exists, the incoming

1 if the method in the @modelattribute tag is saved in the map, and key and 1 key are identical, they are acquired.

3. If there is no key object in the Implicitmodel, check to see if the current handler uses the @sessionattributes annotation modifier, and if the annotation is used, The value attribute of the @sessionattributes annotation contains key, then the value of the key should be obtained from the httpsession, and if there is a direct incoming parameter of the target method, the exception will be thrown if it does not exist.

4. If handler does not indicate that the value of the @sessionattributes annotation or @sessi annotation does not contain a value, then the parameter of the Pojo type is created by reflection as an argument to the target method.

5.SpringMVC saves the key and Pojo type objects to the Implicitmodel, which is then saved to the request.

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.