2. running process after using paramsprepareparamsstack interceptor Stack
1). paramsprepareparamsstack and defaultstack are both interceptor stacks, while the Struts-default package uses defastack stack by default.
2) You can modify the default interceptor stack in the struts configuration file in the following ways:
<Default-interceptor-ref name = "paramsprepareparamsstack"> </default-interceptor-ref>
3) paramsprepareparamsstack interceptor lies in
Params-> modeldriven-> Params
You can first assign the request parameter to the corresponding attribute of the action, then press the attribute value assigned to the action to the object at the top of the value stack, and then assign a value to the attribute of the object at the top of the stack.
For the edit operation:
1. assign a value to the employee ID of the employee action first.
2. Load the corresponding objects from the database according to the employeeid and place them to the top of the stack of the value stack.
3. assign a value to the employee ID of the stack top object (in fact, the value of the employee ID attribute already exists)
4. display the attributes of the stack top object in the form.
5. After the form data is modified, submit the data. At this time, all the data is placed in the top object of the stack.
6. Therefore, we need the modeldriven interface to convert the stack top object into our object.
4). Echo: The struts2 form tag obtains the corresponding attribute values from the value stack for ECHO.
5). Problems:
GetModel Method
Public Employee GetModel (){
If (employeeid = NULL)
Employee = new employee ();
Else
Employee = Dao. Get (employeeid );
Return employee;
}
I. During deletion, the employee ID is not null, but the GetModel method loads an object from the database. It should not be loaded!
Ii. When pointing to querying all information, it is also a new employee () object. a waste!
6). Solution: Use the prepareinterceptor and preparable interfaces.
7). About prepareinterceptor --- for this interceptor, it is used to prepare the modeldriven Interceptor. If prepare [actionmethodname] () or prepare () is called ()
---- Method, the GetModel () method of modeldriven will not be executed again,
[Conclusions after analysis]
If action implements the preparable interface, Struts will try to execute the prepare [actionmethodname] method,
If prepare [actionmethodname] does not exist, the preparedo [actionmethodname] method will be executed.
If none of them exist, they will not be executed.
If the alwaysinvokeprepare attribute of prepareinterceptor is false (true by default ),
Struts2 will not call the prepare () method of the action that implements the preparable interface.
[Solutions to 5) problems]
You can prepare the prepare [actionmethdname] method for each actionmethod, and discard the original prepare () method.
Set the alwaysinvokeprepare attribute of prepareinterceptor to false to prevent the struts2 framework from calling the prepare () method.
How to assign a value to the interceptor stack attribute in the configuration file: see/Struts-2.3.15.3/docs/ww/docs/interceptors.html
<Interceptors>
<Interceptor-stack name = "parentstack">
<Interceptor-ref name = "defaultstack">
<Param name = "Params. excludeparams"> token </param>
</Interceptor-ref>
</Interceptor-stack>
</Interceptors>
<Default-interceptor-ref name = "parentstack"/>
---------------------------------- Source code parsing ---------------------------------
Public String dointercept (actioninvocation Invocation) throws exception {
// Obtain the action instance
Object action = invocation. getaction ();
// Determine whether action implements the preparable Interface
If (Action instanceof preparable ){
Try {
String [] prefixes;
// Determine prefixes Based on the firstcallpreparedo (false by default) attribute of the current interceptor
If (firstcallpreparedo ){
Prefixes = new string [] {alt_prepare_prefix, prepare_prefix };
} Else {
Prefixes = new string [] {prepare_prefix, alt_prepare_prefix };
}
// If it is false, prefixes: Prepare, preparedo
// Call the prefix method.
Prefixmethodinvocationutil. invokeprefixmethod (invocation, prefixes );
}
Catch (invocationtargetexception e ){
Throwable cause = E. getcause ();
If (cause instanceof exception ){
Throw (exception) cause;
} Else if (cause instanceof error ){
Throw (error) cause;
} Else {
Throw E;
}
}
// Determine whether to call the prepare method of action based on the alwaysinvokeprepare (true by default) of the current interceptor.
If (alwaysinvokeprepare ){
(Preparable) Action). Prepare ();
}
}
Return invocation. Invoke ();
}
Prefixmethodinvocationutil. invokeprefixmethod (invocation, prefixes) method:
Public static void invokeprefixmethod (actioninvocation, string [] prefixes) throws invocationtargetexception, illegalaccessexception {
// Obtain the action instance
Object action = actioninvocation. getaction ();
// Obtain the name of the Action method to be called (update)
String methodname = actioninvocation. getproxy (). getmethod ();
If (methodname = NULL ){
// If null returns (possible according to the docs), use the default Execute
Methodname = default_invocation_methodname;
}
// Obtain the prefix
Method method = getprefixedmethod (prefixes, methodname, action );
// If the method is not null, the prefix method is called through reflection.
If (method! = NULL ){
Method. Invoke (action, new object [0]);
}
}
Prefixmethodinvocationutil. getprefixedmethod method:
Public static method getprefixedmethod (string [] prefixes, string methodname, object action ){
Assert (prefixes! = NULL );
// Converts the first letter of the method to uppercase.
String capitalizedmethodname = capitalizemethodname (methodname );
// Traverse the prefix Array
For (string prefixe: prefixes ){
// Obtain the prefix method name by splicing: prepareupdate for the first time and preparedoupdate for the second time.
String prefixedmethodname = prefixe + capitalizedmethodname;
Try {
// Obtain the corresponding method from the action using reflection. If any, return directly and end the loop.
Return Action. getclass (). getmethod (prefixedmethodname, empty_class_array );
}
Catch (nosuchmethodexception e ){
// Hmm -- OK, try next prefix
If (log. isdebugenabled ()){
Log. debug ("cannot find method [#0] in action [#1]", prefixedmethodname, action. tostring ());
}
}
}
Return NULL;
}
Running process after paramsprepareparamsstack interceptor stack is used