Document directory
- Actionform should be categorized into controllers according to official documents
- Notes about action:
- Actionmapping --> encapsulate <action-mapping> In Struts-config
- Actionforward (Navigator)
- How actionform works: general steps for processing actionform
Based on the working principles of the previous blog, let's take a look at the analysis of a specific small example and use struts to add students, such:
A simple example: Download the source code: Ghost.
Actionform should be categorized into controllers according to official documents
Gorgeous split line ----------------------------------------------------------------------------------------------------------------
Notes about action:
1. When will action be initialized?
It is initialized when the user sends this action request, rather than reading the configuration.
2. How many initialization times?
Initialization is performed once in total, and only once at the first request of the user, regardless of how many types of actions in the struts-config.xml point to the action class.
3. If the types in the configuration of two actions point to the same action, the action is initialized only once. That is to say, multiple action requests enter the same action class, which may cause thread security problems, how can this problem be solved?
* If this information is only used for a request, we cannot use instance variables or class variables. On the contrary, if all my requests need to share some information, you can use instance variables or static variables to save
* Access to other resources (such as JavaBean and session) must be synchronized if these resources need to be protected.
* The difference between struts and struts2 is that action security and anxiety are a big difference.
* Let's take a simple example: count the number of visits to an action. The security issues are generated and solved as shown in the figure:
Gorgeous split line ----------------------------------------------------------------------------------------------------------------
Actionmapping --> encapsulate <action-mapping> In Struts-config
Gorgeous split line ----------------------------------------------------------------------------------------------------------------
Actionforward (Navigator)
Redirect:
False --> jump to the container. requestdispatcher: Forward
True --> out-of-container jump. httpresponse: sendredirect. absolute paths are required to jump, such as Path = http://www.baidu.com
Gorgeous split line ----------------------------------------------------------------------------------------------------------------
How actionform works: general steps for processing actionform
1. Check the action ing to confirm that the actionform ing has been configured in the action.
2. query the configuration information of formbean Based on the name attribute.
3. Check the usage range of formbean of action and determine whether the formbean instance has been used in this range (request, session ).
4. If the formbean instance already exists in the current range and is of the same type for the current request, it will be reused.
5. Otherwise, create a new formbean instance (call the constructor) and save the instance to a certain extent.
6. the reset method of formbean is called.
7. Call the corresponding setter method and assign values to the status attribute.
8. If the validate attribute is set to true, The validate method of formbean is called.
9. If no error is returned for the validate method, the controller uses actionform as a parameter and passes it to the execute method of the action instance and runs
Note: The reset and validate methods inherited directly from the actionform class cannot implement any processing functions, so it is necessary to overwrite them by yourself.
The figure also shows that the verification is executed before the execute method after the value is assigned.
How can I prove that the form is saved in the above flowchart? In scope. setattribute (name, form)
1. process-based monitoring
Httpsessionattributelistener
Servletrequestattributelistener
The above two interfaces can capture attribute dynamics in different situations where scope is request and session.
public class AttributeListener implements HttpSessionAttributeListener,ServletRequestAttributeListener {@Overridepublic void attributeAdded(ServletRequestAttributeEvent arg0) {System.out.println("add request attribute");if(arg0.getValue() instanceof ActionForm){System.out.println("this is request add mothed");System.out.println(arg0.getName()+","+arg0.getValue());}}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent arg0) {System.out.println("Removed request attribute");if(arg0.getValue() instanceof ActionForm){System.out.println("this is request Removed mothed");System.out.println(arg0.getName()+","+arg0.getValue());}}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent arg0) {System.out.println("Replaced request attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is request Replaced mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeAdded(HttpSessionBindingEvent arg0) {System.out.println("Add session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Add mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeRemoved(HttpSessionBindingEvent arg0) {System.out.println("Removed session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Removed mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeReplaced(HttpSessionBindingEvent arg0) {System.out.println("Replaced session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Replaced mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}}
Here we only care about actionform
You can see in the action configuration file:
<action path="/add" type="com.pccw.action.AddStudentAction" name="studentForm"><forward name="addsuccess" path="/addsuccess.jsp"></forward><forward name="addfail" path="/addfail.jsp"></forward></action>
The scope is not set here. The default value is session, so we can see the printed information when the request comes over:
This is monitored in the add method of the session.
2. view the results
Get the actionform directly from the request in execute, and then compare it with the form parameter in execute, we will find that these two items are the same thing.
Public class addstudentaction extends action {@ overridepublic actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {studentform = (studentform) form; studentdao Dao = new studentdaoimpl (); Boolean issave = Dao. addstudent (studentform); string returnkeyvalue = "addfail"; if (issave) returnkeyvalue = "addsuccess"; studentform studentform2 = NULL; string scope = mapping. getscope (); If (scope. equals ("request") {studentform2 = (studentform) request. getattribute ("studentform");} else {studentform2 = (studentform) request. getsession (). getattribute ("studentform");} system. out. println (studentform = studentform2); // One is a parameter and the other is the return mapping obtained from the request. findforward (returnkeyvalue );}}
The following figure shows the printed result:
If the print result is true, it indicates that the content is not closely the same and the address is the same.