In Springmvc, we can implement the effects of preparable interceptors similar to those in Struts2 by using the @modelattribute annotation tagging method, which we have already described in how to handle model data in SPRINGMVC.
Now we still take the update operation in the above article as an example to discuss the working process of @modelattribute. That is: There is a user class, with ID, userName, email three properties. Now to complete an update operation, but one of the properties can not be modified, such as ID, then can only modify two properties, username and email, so the information passed from the form form can only have these two items, We do this through the @modelattribute annotation tagging method: (where database-related operations are only simulated)
INDEX.JSP:
<!--
Simulation modification Operation
1. The original data is: 1,jack,jack@163.com
2. Modifying username and email to Mike, Mike@qq.com,id cannot be modified.
3. Form ECHO, simulation operation directly fill in the form corresponding attribute value--
<form action= "Springmvc/testmodelattribute" method= "Post" >
Username: <input type= "text" name= "username" value= "Jack"/>
<br>
Email: <input type= "Text" Name= "Email" value= "Jack@163.com"/>
<br>
<input type= "Submit" value= "Submit"/>
</ Form>
Controller
@ModelAttribute public
void GetUser (map<string, object> Map) {
System.out.println ("Modelattribute Method ");
Impersonation gets the object from the database
user user = new User (1, "Jack", "Jack@163.com");
System.out.println ("Get an object from the database:" + user);
Map.put ("user", user);
}
@RequestMapping ("/testmodelattribute") public
String testmodelattribute (user user) {
System.out.println (" Modified: "+ user";
return SUCCESS;
}
After running, in the console output:
In this example, SPRINGMVC calls the target processing method Testmodelattribute method by mapping the requestbefore, do the following three things:
1th Step:Perform @ModelAttribute annotation retouching method: Remove the object from the database, put the object into the Map, the key is: User.
2nd Step:Springmvc the user object from the MAP and assigns the request parameter of the form to the user's corresponding property.
3rd Step:Springmvc passes the above object as a parameter to the target method Testmodelattribute (user user).
By reading the source code of SPRINGMVC, I understand that these three steps are specifically implemented as follows:
1th Step:Perform a method @ModelAttribute annotation adornments,In fact, the data user in the Map in @ModelAttribute method is placed in the Implicitmodel (this is an object of type Bindingawaremodelmap, The Bindingawaremodelmap type implements the map interface)。
2nd Step:The core functions are implemented in step 2nd, which is done in three things, the core of which is the first two things, namely 2.1 and 2.2:
2.1 (First thing): Determine the key that is required to find the key value pair:
2.1.1:If the parameter of the POJO type of the target methoddo not use @ModelAttribute as adornments, the key is POJOclass name The first letter of lowercase;
2.1.2:If the parameter of the POJO type of the target methodused @modelattribute to decorate the, the key isvalue of @ModelAttribute annotation。
In the example above, the entry parameter in the target method Testmodelattribute (user user) is not @modelattribute decorated, so the key value is the lowercase form of the first letter of the Pojo class name, which is "User".
2.2 (Second thing): Find the object of key in Implicitmodel (that is, map):
2.2.1:If the method of @ModelAttribute tag is in Maphas been savedSuch a key-value pair,its key is consistent with key 2.1, the value of the key value pair corresponding to key is obtained;
2.2.2:If the Implicitmodeldoes not existKey corresponds to the object, check the currentwhether the controller class is @sessionattributes annotation decorated,if the @sessionattributes annotation decoration is used,and the value of the @sessionattributes annotation contains the key, you try to get the value of the key from the HttpSession,gets the value if it exists,throws an exception if the value does not exist。
2.2.3:Ifthe controller class is not decorated with @sessionattributes annotationsOrused, but the value in the @sessionattributes annotation does not contain a key, the SPRINGMVC willto create an object of type Pojo by reflection。
2.3 (Third thing):, use the parameter values passed in the form to update the value of 2.2 (or the object established by reflection).
3rd Step:As described in the 3rd step above, Springmvc passes the above object as a parameter into the target method.
The above is the workflow of @modelattribute annotations, we understand the process, we will find that in 2.2.2, there is a situation: if:
① does not exist in the Implicitmodel of the key corresponding to the object;
The ② controller class has tagged @sessionattributes annotations;
The value of the ③ @SessionAttributes annotation contains key;
The value corresponding to the key value does not exist in the ④httpsession domain.
When these 4 cases are met, an exception is thrown and there are two workarounds:
1. The @modelattribute annotation modifies the entry parameter of the target method to determine a key value so that it is not included in the value of the @sessionattributes annotation.
2, using the @modelattribute method, before the target method call, the key value corresponding to the pair into the Implicitmodel.