"Struts2": Configuration and use of interceptor

Source: Internet
Author: User

In the previous blog, introduced the Struts2 of the built-in interceptors and custom interceptors some content, but with these self-contained and custom interceptors, how to configure and use, have not explained to you in detail, then we will learn together, Configuration and use of interceptor in STRUTS2.


The Interceptor configuration information in the STRUTS2 framework is written in the configuration file Struts.xml, and usually we simply specify an interceptor name for the Interceptor class and the interceptor definition is complete. The definition interceptor is defined using the <interceptor.../> element, and its simplest format is as follows;

<span style= "FONT-SIZE:18PX;" >        <!--define interceptors by specifying interceptor name and Interceptor implementation Class--
<span style= "White-space:pre" >	</span>< Interceptor Name= "Interceptor name" class= "Interceptor Implementation Class" >

	</interceptor></span>

If you also need to pass in parameters when you configure the interceptor, you need to use the <param.../> child element in the <interceptor.../> element for the parameter setting, which sets the default parameter value for the Interceptor parameter. The specific format is as follows;

<span style= "FONT-SIZE:18PX;" ><span style= "WHITE-SPACE:PRE;" >	</span><!--define interceptors by specifying interceptor name and Interceptor implementation Class--
	<interceptor name= "Interceptor name" class= "Interceptor Implementation Class" >
	<!--specifying the parameter value for the Interceptor--
		<param name= parameter name > argument value </param>
	</interceptor></span>

In addition, you can put multiple interceptors together to form an "interceptor stack", for example, if you want to do a login check, security check, and logging, and so on before the action executes, you can make an interceptor stack of the three corresponding interceptors. Defining the Interceptor stack using the <interceptor-stack.../> element, the interceptor stack is made up of multiple interceptors, so you need to use the <interceptor-ref in the <interceptor-stack.../> element ... The/> element defines a reference to multiple interceptors, that is, the interceptor stack consists of interceptors specified by multiple <interceptor-ref.../> elements.


How to understand the interceptor stack. From a single structure, the interceptor stack is composed of multiple interceptors, but from a functional point of view, the Interceptor and the interceptor stack are the same, is to complete the action to intercept, before the action execution method executes automatically, in fact, the interceptor stack is a larger interceptor just. The configuration information for the interceptor stack is as follows:

<span style= "FONT-SIZE:18PX;" ><span style= "WHITE-SPACE:PRE;" >	</span><interceptor-stack name= "Interceptor stack name" >
		<interceptor-ref name= "Interceptor One"/>
		< Interceptor-ref name= "Interceptor Two"/>
		<!--can also be configured with more interceptors--
		...
	</interceptor-stack></span>
Above we mentioned that the Interceptor stack and interceptor is essentially a thing, so the interceptor stack can also be nested or contain the interceptor stack, such as the following configuration information example of the interceptor stack two refers to the interceptor stack one:
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" >	</span><interceptor-stack name= "Interceptor stack 1" >
		< Interceptor-ref name= "Interceptor One"/>
		<interceptor-ref name= "Interceptor Two"/>
		<!--can also be configured with more interceptors--
		...
	</interceptor-stack>
			
	<interceptor-stack name= "Interceptor Stack 2" >
		<interceptor-ref name= "Interceptor One "/>
		<interceptor-ref name=" Interceptor stack 1 "/>
		<!--can also configure more interceptors--
		...
	</interceptor-stack></span>
A big advantage of this is the software reuse, according to the requirements of the software to classify the interceptor, composed of different interceptor stacks, and then in the program development according to different requirements to directly invoke the response of the interceptor stack, so that you do not have to call so many interceptors, flexible combination can be.

Once you have defined the interceptor and interceptor stack, you can use the interceptor or the interceptor stack to intercept the action, and the Interceptor's interception behavior will be executed before the action execution method executes. The <interceptor-ref.../> element allows you to use interceptors within an action, with the same configuration syntax as referring to interceptors within the interceptor stack. The following example shows how to configure the Interceptor and interceptor stacks in action.

<span style= "FONT-SIZE:18PX;" ><span style= "WHITE-SPACE:PRE;" > </span><!--Configure the system using the package--<package name= "Lee" extends= "Struts-default" > <!--applications need to use the interceptors are in that meta Configuration--<interceptors> <!--configuring Mysimple interceptors--<interceptor name= "mysimple" class= "Org.ljw.ap P.interceptor.simpleinterceptor "> <!--Specify parameter values for interceptors--<param name=" name "> Simple interceptors </param> < /interceptor> </interceptors> <action name= "Login" class= "org.ljw.app.action.LoginAction" > <resu Lt name= "error" >/WEB-INF/content/error.jsp</result> <result>/web-inf/content/welcome.jsp</ Result> <!--configuration system default Interceptor--<interceptor-ref name= "Defaultstack"/> <!--Apply custom Mysimple Interceptor--&G
			T <interceptor-ref name= "Mysimple" > <param name= "Name" > renamed Interceptor </param> </interceptor-ref> & lt;/action> <action name= "*" > <result>/web-inf/content/{1}.jsp</result> </action> </package></span> 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.