English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

struts2Process and a Series of Related Knowledge Code Analysis

1.The client initializes a request pointing to the servlet container (Tomcat);

2.This request goes through a series of filters, followed by the invocation of FilterDispatcher;

3.FilterDispatcher asks ActionMapper to decide whether this request should call an action;

4.If ActionMapper decides to call an Action, FilterDispatcher hands over the request handling to ActionProxy, ActionProxy queries the framework's configuration file through ConfigurationManager to find the Action class to be called, which is usually reading struts.xml;

5.The ActionProxy creates an instance of ActionInvocation, which uses the naming pattern to call. Before and after the call to the Action, related interceptors are involved;

6.Once the Action execution is complete, the ActionInvocation finds the corresponding return result based on the configuration in struts.xml;

For example, the code:

struts2After obtaining the .action request, it will decide which business logic component to call based on the configuration part;

struts2All the Actions in the application are defined in struts.xml;

struts2The Action instance used to process user requests is not the business controller implemented by the user, but an Action proxy, because the business controller implemented by the user is not coupled with ServletAPI, and it is obviously unable to 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 action attribute of the above form is not a normal servlet nor a dynamic JSP page. When the form is submitted to hello.action, Struts2FilterDispatcher will take effect, forwarding the user request to the corresponding Action.

Note that: Struts2 The Action default intercepts all requests with the suffix .action. If we need to submit a form for processing by Action, we should set the form action attribute 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, it is only the page forwarding that has been executed, without tracking the user's status. After the user logs in, we need to add the username of the user as the status information of the HTTPSession.

To access the Httpsession instance, struts2It provides an ActionContext class that provides a getSession() method, but the return value of this method is not HttpSession() but Map(), but Struts2interceptor is responsible for the switch between Session() and HttpSession().

To check if the session attribute we set is successful, we can set the interface like this after the success

<html>
 <head>
  <base href="<%=basePath%>" rel="external nofollow" >
  <title>SUCCESS</title>
 </head>
 <body>
  Welcome, ${sessionScope.user}, you are already logged in.
 </body>
</html>

Using JSP2.0 expression syntax to output the user attribute in the HTTP Session.

The Action utility class integrates ActionSupport

The ActionSupport class is a utility class and has already implemented the Action interface. In addition, it also implements the Validateablez interface, providing data validation functionality.

To enhance the input data validation functionality, the validate method is added and overridden in the 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 data validation errors are already included in the fieldError of the Action class after executing this method, the request will be forwarded to the input logic view, so you still need to add the input logic view name in struts.xml to redirect to the login page.

The disadvantage of this validate method is that it needs to rewrite the validate method in large quantities, so it can use struts2validation framework for validation.

<?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>

Summary

That is all about struts in this article2The full content of the process and a series of related knowledge code analysis, hoping to be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave a message if there are any shortcomings. Thank you for your support to this site!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like