1. The client initializes a request to the servlet container (Tomcat);
2. This request passes through a series of filters, and then FilterDispatcher is called;
3. FilterDispatcher asks ActionMapper to decide whether this request wants to call a certain action;
4. If ActionMapper decides to call an Action, FilterDispatcher handes the request processing to ActionProxy. ActionPro asks the framework configuration file according to the ConfigurationManager and finds the Action class that needs to be called, generally reading struts.xml;
5. ActionProxy creates an instance of ActionInvocation. The ActionInvocation instance is called using a named pattern. Before and after the process of calling Action, the call of the relevant interceptor is involved;
6. Once the Action is executed, ActionInvocation finds the corresponding return result according to the configuration in struts.xml.
For example code:
After struts2 obtains the .action request, it will decide which business logic component to call based on the partial;
All Actions in struts2 applications are defined in struts.xml;
The Action instance used by struts2 to handle user requests is not a user-implemented business controller, but an Action proxy, because the service controller implemented by the user is not coupled with the ServletAPI, and obviously cannot handle user requests.
<html> <head> <title>SUCCESS</title> </head> <body> <form action="hello.action" method="post"> USERNAME:<input type="text" name="name"></br> PASSWORD:<input type="password" name="pass"></br> <input type="submit" value="submit"> </form> </body></html>
For example, the hello.action of the form above, this action property is not an ordinary servlet or a dynamic JSP page. When the form is submitted to hello.action, the FilterDispatcher of Struts2 will work and forward the user request to the corresponding Action.
Note that Struts2 Action intercepts all requests with suffixes .action by default. If we need to submit the form to Action for processing, the form action attribute should be set to the format of .action.
Controller class
public class HelloAction {private String name;private String pass;public void setName(String name){this.name=name;}public void setPass(String pass){this.pass=pass;}public String execute(){if("yang".equals(name) && "1234".equals(pass)){return "success";} else{return "error";}}}After the previous execution is completed, the page forwarding is only performed, and the user's status is not tracked. When the user logs in, we need to add the user's user name as the status information of the HTTPSession.
In order to access the Httpsession instance, struts2 provides an ActionContext class, which provides a getSession() method, but the return value of this method is not HttpSession() but Map(), but the interceptor of Struts2 will be responsible for switching between the Session() and HttpSession().
In order to check whether the session attribute we set is successful, we can set the interface after success
<html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>SUCCESS</title> </head> <body> Welcome, ${sessionScope.user}, you are already logged in. </body></html>Use JSP2.0 expression syntax to output the user attribute in HTTP Session.
Action Tool Class Integration ActionSupport
The ActionSupport class is a tool class, and it has implemented the Action interface. In addition, it also implements the Validateablez interface, providing data verification function.
In order to increase the verification function of input data, add the rewrite validate method in Action.
public void validate() {if(getName()==null || getName().trim().equals("")){addFieldError("name",getText("name.required"));}if(getPass()==null || getPass().trim().equals("")){addFieldError("pass", getText("pass.required"));}}The rewritten validate method added above will be executed before the system's execute() method. If the fieldError of the Action class already contains data verification errors after executing this method, the request will be forwarded to the input logical view, so you also need to add the input logical view name in struts.xml to let it jump to the login page.
The disadvantage of this validate method is that it requires a lot of rewriting of the validate method, so you can use the verification framework of struts2.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"><validators><!--Verify form name--> <field name="name"> <field-validator type="requiredstring"> <message key="name.required"/> </field-validator> </field> <!--Verify form pass--> <field name="pass"> <field-validator type="requiredstring"> <message key="pass.required"/> </field-validator> </field></validators>
Summarize
The above is the entire content of this article about the process of struts2 and a series of related knowledge code analysis. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!