Preface
The interceptor system is an important part of the Struts2 framework. It is no exaggeration to say that without the interceptor system, there will be no such easy-to-use Struts2 framework. In the Struts2 framework, a large number of interceptors have completed many basic functions. For example, the params interceptor is responsible for parsing the parameters of HTTP requests and setting the properties of Action; the servlet-config interceptor directly passes the HttpServletRequest instance and HttpServletResponse instance in the HTTP request to Action; the fileUpload interceptor is responsible for parsing the file domain in the request parameters and setting a file domain to the three properties of the Action... All of this is done by the built-in interceptor. Therefore, mastering the principles and methods of using interceptors in Struts2, we can grasp the "lifeline" of the Struts2 framework.
However, until now, apart from configuring a StrutsPrepareAndExecuteFilter filter in web.xml, we have not yet contacted any other interceptors. So why can our application run well? In fact, Struts2 has enabled a large number of common interceptors by default. These interceptors will work as long as the package configured for Action inherits the struts-default package. Let’s take a look at the built-in interceptor of Struts2.
Struts2 built-in interceptor
There are many built-in interceptors in the Struts2 framework. These interceptors almost complete 70% of the work of the Struts2 framework, including parsing request parameters, assigning request parameters to Action attributes, etc. The clever design of Struts2 is largely due to the design of the interceptor; when it is necessary to extend the Struts2 function, you only need to provide the corresponding interceptor and configure it in the Struts2 container.
These built-in interceptors are configured in the struts-default.xml file in the form of name-class pairs, where name is the name of the interceptor, which is the unique identifier of the interceptor to be used later; class specifies the implementation class of the interceptor. For a detailed description of these built-in interceptors, please refer to the official documentation.
Configure the interceptor
Defining an interceptor in the Struts.xml file only requires specifying an interceptor name for the interceptor class, and the interceptor definition is completed. Define an interceptor using <interceptor.../>, for example:
<!-- Define the interceptor by specifying the interceptor name and the interceptor implementation class--><interceptor name="interceptor name"> <param name="param name">param value</param></interceptor>
In addition, multiple interceptors can be connected together to form an interceptor stack, and <interceptor-ref .../> is used in the interceptor to define the interceptor reference. For example:
<interceptor-stack name="interceptor stack one"> <interceptor-ref name="interceptor one"/> <interceptor-ref name="interceptor two"/> ...</interceptor-stack>
From the perspective of program structure, the interceptor stack consists of multiple interceptors; but from the perspective of program function, the interceptor stack and interceptor are the same, and the methods they contain will be automatically executed before the execution of the Action execute method. Therefore, we can completely treat the interceptor stack as a larger interceptor.
Since the interceptor stack and the interceptor are consistent, the interceptor stack can also contain an interceptor stack, for example:
<interceptor-stack name="interceptor stack 2"> <interceptor-ref name="modelDriven"/> <interceptor-ref name="interceptor stack 1"/></interceptor-stack>
Using interceptor
Once the interceptor stack and interceptor are defined, this interceptor stack or interceptor can be used to intercept the Action. The interceptor's interception behavior will be executed before the action executes.
By using the <interceptor-ref .../> element, the configuration syntax of using an interceptor in an Action is exactly the same as the syntax of referencing an interceptor when configuring the interceptor stack. For example:
<action name="login"> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> <!-- Interceptor stack--> <interceptor-ref name="defaultStack"/> <!-- Interceptor-ref name="test1"/> <!-- Interceptor with parameters--> <interceptor-ref name="test2"> <param name="key">Dynamic parameters</param> </interceptor-ref></action>
After this configuration is completed, these three interceptors will work before the DownloadAction is executed.
Configure the default interceptor
When configuring a package, it can be specified with a default interceptor. Once a default interceptor is specified for a package, the default interceptor will work if the Action in that package does not explicitly specify the interceptor. However, once we explicitly apply an interceptor to the Action in the package, the default interceptor will not work; if the Action requires the default interceptor to be used, you must manually configure the reference to the interceptor.
The default interceptor for the package where the Action is located will take effect only if there is no explicitly applied in the Action.
Configure the default interceptor to use the <default-interceptor-ref.../> element, which is used as a child of the <package.../> element, and configure the default interceptor for all Actions under this package. For example:
<default-interceptor-ref name="default interceptor"/>
You can also specify parameters for the default interceptor, for example:
<default-interceptor-ref name="default interceptor"> <param name="parameter name">parameter value</param></default-interceptor-ref>
In the struts-default.xml file, an abstract package named struts-default is configured, in which a default interceptor reference named defaultStack is defined. When the package we define inherits the struts-default package, it also inherits its default interceptor stack: defaultStack, which also means that if we do not explicitly apply the interceptor for Action, the defaultStack interceptor stack will take effect automatically.
Custom interceptor
1>. Add a class to inherit the AbstractInterceptor class, or implement the Interceptor interface
public class TimeInterceptor extends AbstractInterceptor {/*** The return value of the core method of the intercept is a string*/@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubreturn "login";}}2>. Add interceptors child node in struts.xml package and add Interceptor node under it
<package name="goods" namespace="/goods" extends="common-pkg"><interceptors><interceptor name="timeInterceptor"></interceptor></interceptors></package>
3>.Add child node interceptor-ref under the action node you want to be intercepted
<action name="list_Category" method="list"><interceptor-ref name="timeInterceptor"></interceptor-ref><result name="list" type="dispatcher">/pages/Category/list.jsp</result></action>
Summarize
This article provides a preliminary summary of the core of Stuts2 - the interceptor. In the subsequent article, we will implement our own interceptor and apply the knowledge points summarized in this article.
Okay, the above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.