In struts (Version 1.1), there is a situation described below:
There are two actions, userloginaction and logindetailaction, and a mylogin. jsp page.
The procedure of the program is as follows:
Request a mylogin. jsp file. In the Name input box, enter Gordian. Click Submit. The process jumps to userloginaction. In userloginaction,
Change the value of form's name attribute to 'gct 'and jump to logindetailaction. At this time, we found that the value of name attribute in form is still Gordian, not in userloginaction
'Gct 'modified in, that is, the form operation is invalid in userloginaction.
Note: Both userloginaction and logindetailaction point to the same form.
After tracking the struts source code, it is found that the method processpopulate () in the requestprocessor class of struts performs a series of key operations on form.
For each request, the struts engine is responsible for generating the form instance and saving the form instance in the request or in session (etc.) based on the scope set in the configuration file.
When a request arrives, the engine first searches for the form name based on the action, and then finds the form instance in the request or session based on the name. If yes, return directly.
If not, a new instance is generated.
After obtaining the instance, the engine calls the method processpopulate () in the requestprocessor class (). This method resets the form attribute value: form. Reset (mapping, request );
The requestutils. populate (Form, mapping. getprefix (), mapping. getsuffix (), request) method is called, and the value pair is retrieved from the request,
Reset the form value. Therefore, the form value is consistent with that in the request. Therefore, any operation on form in userloginaction is invalid.
According to this inference, even if userloginaction and logindetailaction point to different forms, the two forms have the same attributes and modify the values of the same attributes in userloginaction,
The value of this attribute obtained in logindetailaction remains unchanged.
Code List 1:
Public class userloginaction extends comtopaction {
Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request,
Httpservletresponse response) throws exception {
Logger. debug ("userloginaction ");
Userform = (userform) form;
Logger. debug (userform );
Logger. debug ("in userloginaction userform. hashcode" + userform. hashcode ());
Userform. setactiontype ("detail ");
Logger. debug ("setting the actiontype value in userloginaction ");
Userform. setname ("GCT ");
Return Mapping. findforward ("logindetailaction ");
}
}
Code List 2:
Public class logindetailaction extends comtopaction {
Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request,
Httpservletresponse response) throws exception {
Userform = (userform) form;
Logger. debug ("logindetailaction ");
Logger. debug (userform );
System. Out. println (userform );
Logger. debug ("in logindetailaction userform. hashcode" + userform. hashcode ());
Return Mapping. findforward ("logindetail ");
}
}
Code List 3:
<HTML>
<Script language = "JavaScript" src = "/web/system/JS/setday. js"> </SCRIPT>
<Title> just for test </title>
<Script language = "JavaScript">
<! --
// Select a department
Function submitdata (form ){
Form. logindate. value = '2014-2-11 ';
// Form. Action = form. Action + '? Actiontype = list ';
Alert (Form. action );
Form. Submit ();
}
// -->
</SCRIPT>
<Body>
<Form action = "/web/product/patrol/userloginaction. Do" name = "userform"/>
Name: <input class = flat_u size = 30 name = "name"/>
<Input type = "hidden" name = "actiontype" value = "list"/>
<Input class = flat_u maxlength = 20 size = 30 name = "logindate" readonly>
<Input name = "button" type = button class = btn_href value = & nbsp; Save & nbsp; onclick = "submitdata (this. Form)">
</Form>
</Body>
</Html>
/**
* Populate the properties of the specified actionform instance from
* The request parameters has ded with this request. In addition,
* Request attribute <code> globals. cancel_key </code> will be set if
* The request was submitted with a button created
* <Code> canceltag </code>.
*
* @ Param request the Servlet request we are processing
* @ Param response the servlet response we are creating
* @ Param form the actionform instance we are populating
* @ Param mapping the actionmapping we are using
*
* @ Exception servletexception if thrown by requestutils. populate ()
*/
Protected void processpopulate (httpservletrequest request,
Httpservletresponse response,
Actionform form,
Actionmapping mapping)
Throws servletexception {
If (form = NULL ){
Return;
}
// Populate the bean properties of this actionform instance
If (log. isdebugenabled ()){
Log. debug ("populating bean properties from this request ");
}
Form. setservlet (this. servlet );
Form. Reset (mapping, request );
If (mapping. getmultipartclass ()! = NULL ){
Request. setattribute (globals. multipart_key,
Mapping. getmultipartclass ());
}
Requestutils. populate (Form, mapping. getprefix (), mapping. getsuffix (),
Request );
// Set the cancellation request attribute if appropriate
If (request. getparameter (constants. cancel_property )! = NULL) |
(Request. getparameter (constants. cancel_property_x )! = NULL )){
Request. setattribute (globals. cancel_key, Boolean. True );
}
}