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.
@RequestMapping ("/updateuser") 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
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:
PackageCom.proc;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam; @Controller Public classUsercontroller {@ModelAttribute 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"); Map.put ("User", user); }} @RequestMapping ("/updateuser") PublicString Update (@ModelAttribute ("ABC") (user user) {System.out.println (The update User object is: "+user); return"Success"; }}
JSP page
<%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"ContentType="text/html; Charset=utf-8"%> <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"> <HTML> <Head> </Head> <Body> <formAction= "UpdateUser"Method= "POST"> <inputtype= "hidden"name= "id"value= "1"> <Table> <TR> <TD>User name:</TD><TD><inputtype= "text"name= "username"value= "Caoyc"/></TD> </TR> <TR> <TD>Age:</TD><TD><inputtype= "text"name= "Age"value= " a"/></TD> </TR> <TR> <TD></TD><TD><inputtype= "Submit"value= "Submit"></TD> </TR> </Table> </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.
Java code
@SessionAttributes (value={"user"}) @Controllerpublicclass Usercontroller { @ Requestmapping ("/updateuser") public String Update (user user) { // System.out.println ("Update User object is:" +user); return "Success"; }}
At this point, if there is no key in the session for the user value, then only/updateuser will throw an exception. Because the discovery class has @sessionattributes annotations when only the target method is required, and the target method requires a Pojo object with the key user, the key also exists in the value of the @sessionattributes annotation, so an exception is thrown. If there is a value for the key user in the session, the value is passed to the target method parameter.
How to solve the exception?
Method One: Add a method that uses @modelattribute annotations, which saves a pojo to Implicitmodel that is a key user
Method Two: Remove @sessionattributes annotations
Method Three: Modify the key value of the target parameter. For example @ModelAttribute (value= "user2") User user
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.
This article transferred from: http://www.cnblogs.com/caoyc/p/5636870.html
Analysis on the process of Pojo in SPRINGMVC