Analysis on the process of Pojo in Spring MVC

Source: Internet
Author: User

SPRINGMVC Determining the entry process of the target method Pojo type

1. Confirm a key:

(1) If the parameter of the Pojo type of the target method does not use @modelattribute as decoration, then the key is Pojo class name first letter lowercase

(2), if modified with @modelattribute, then key is the value of the @modelattibute annotation.

1 @RequestMapping ("/updateuser")2 public String Update (user user) {

The @modelattribute modifier is not used here, all the corresponding key is the Pojo class first letter lowercase, that is: User

1  Public String Update (@ModelAttribute ("abc") User user)

Here the @modelattribute is used to modify the Pojo, then the corresponding value can be @modelattribute, that is: ABC

  

2. In Implicitmodel, find the object corresponding to key. If present, it is passed as a parameter

(1), if the @modelattibute tag method is stored in the map Pojo, and key and 1 confirm that the key is consistent, it will be obtained. For example:

1  PackageCom.proc;2 3 ImportJava.util.Map;4 5 ImportOrg.springframework.stereotype.Controller;6 ImportOrg.springframework.web.bind.annotation.ModelAttribute;7 Importorg.springframework.web.bind.annotation.RequestMapping;8 ImportOrg.springframework.web.bind.annotation.RequestParam;9 Ten @Controller One  Public classUsercontroller { A  -      - @ModelAttribute the      Public voidGetUser (@RequestParam (required=false) Integer Id,map Map) { -         if(id!=NULL){ -User user=NewUser (); -User.setid (1); +User.setusername ("Caoyc"); -User.setage (18); +User.setpassword ("123456"); AMap.put ("User", user); at         } -     } -      -@RequestMapping ("/updateuser") -      PublicString Update (@ModelAttribute ("ABC") (user user) { -SYSTEM.OUT.PRINTLN ("Update User object is:" +user); in         return"Success"; -     } to}

JSP page

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"ContentType="text/html; Charset=utf-8"%>2 3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">4 <HTML>5   <Head>6 7   </Head>8   9   <Body>Ten     <formAction= "UpdateUser"Method= "POST"> One         <inputtype= "hidden"name= "id"value= "1"> A         <Table> -             <TR> -                 <TD>User name:</TD><TD><inputtype= "text"name= "username"value= "Caoyc"/></TD> the             </TR> -             <TR> -                 <TD>Age:</TD><TD><inputtype= "text"name= "Age"value= " a"/></TD> -             </TR> +             <TR> -                 <TD></TD><TD><inputtype= "Submit"value= "Submit"></TD> +             </TR> A            </Table> at     </form> -   </Body> - </HTML>

Result in console output: User [Id=1, Username=caoyc, password=123456, age=12]

The reason for this is: when the method/updateuser the corresponding method update (user user), the first to execute the @modelattribute tag method, In @modelattribute, a Pojo object with a key of user is saved to Implicitmodel. In this case, the key for the parameter in the Update object is the Pojo class name lowercase, and the user, that is, the key and the corresponding Pojo object in the Implicitmodel. The object is then used as the original value. The Pojo object property passed in the JSP page is not empty and has a changed property to modify the original value.

Original object: User [Id=1, Username=caoyc, password=123456, age=18]

JSP: User[id=1, Username=caoyc, Password=null, age=12]

The property that needs to be modified here is age. Modified to change age to 12 based on original merit

So the most always in Implicitmodel the key value for user Pojo object is: User [id=1, Username=caoyc, password=123456, age=12]

  

3, the block Implicitmodel does not exist the object corresponding to the key, then check whether the current handler is using the @sessionattributes annotation decoration, if the annotation is used, And a key is included in the value of the @sessionattribute annotation, the value of the value corresponding to the key is obtained from the httpsession, and if it exists, it is passed directly to the input parameter of the target method and throws an exception if it does not exist.

4, if handler does not identify the @sessionattributes annotation or @sessionattributes annotation value value does not contain the key, it will be reflected to create a parameter of the Pojo type, and as a parameter passed to the target method

5. SPRINGMVC will save the key and Pojo type objects into the Imlicitmodel, which will be saved to the request.

Analysis on the process of Pojo in Spring MVC

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.