The use of interceptor interceptor in STRUTS2

Source: Internet
Author: User

The Struts2 Interceptor (Interceptor) is a very important concept, and most of the functions in STRUTS2 are implemented through interceptors. Here's how to implement the Interceptor in Struts2 and try to customize an interceptor.


principle of Interceptor in 1 Struts2

Before the action is run, Struts2 invokes a large stack of interceptors that call the user-defined action after all the interceptors have been called. As shown in the following illustration, the code runs as follows during the STRUTS2 run: A. First run to the Strutsprepareandexecutefilter class, which is the struts2filter of the following figure;
B. Then call the dispatcher Serviceaction () method;
C. Dispatcher constructs the Actionproxy object and invokes the Execute () method of the proxy;
D. Actionproxy invokes the Invoke method of the Actioininvocation object when executing the Execute () method;
The Invoke () method of the E. Actioininvocation object takes out all the interceptor sequentially and executes the Interceptor intercept () method. (Key steps)
The Intercept () method of F. Interceptor, at the end of execution, recalls the Invoke () method of the actioninvocation so that it can continue to perform the subsequent interceptor.
G. When all interceptor execution is complete, invoke () of the Actioininvocation object invokes our custom action.


2 A custom Struts2 interceptor

A custom interceptor can refer to Struts2 source code, how to define the source code, how we define it.
2.1 Definition Interceptor

The definition interceptor can inherit the Absactintercepor abstract class or implement the Intercepor interface, where I implement the Intercepor interface to calculate the execution time of the action, as follows:

public class Myinterceptor implements interceptor {public

	void Destroy () {
	} public

	void init () {
	}

	Public String intercept (actioninvocation invocation) throws Exception {
		long start = System.currenttimemillis ();
		
		String r = Invocation.invoke ();
		
		Long end = System.currenttimemillis ();
		
		SYSTEM.OUT.PRINTLN ("Action time =" + (End-start)); Calculate the execution time of the action return
		
		r
	}
}
The Invocation.invoke () method must be called in The Intercept method, or the following interceptor and action cannot be executed.


2.2 The Department interceptor

The deployment interceptor is deployed in Struts.xml.

First you have to declare the interceptor:

<interceptors>
	<interceptor name= "My" class= "Cn.edu.MyInterceptor" ></interceptor>
</interceptors>
Second, you need to add the interceptor to the action:

<interceptor-ref name= "My" ></interceptor-ref>
<interceptor-ref name= "Defaultstack" ></ Interceptor-ref>
By default, the Struts2 interceptor is defaultstack, and we need to negotiate two interceptors (custom and Defaultstack) when we have customized the interceptor and should not overwrite the original interceptor. Like the code above.

Its complete struts.xml is as follows:

<struts>
	<constant name= "Struts.devmode" value= "true" ></constant>
	<package name= " Test "namespace="/"extends=" Struts-default ">
		<interceptors>
			<interceptor name=" my "class=" Cn.edu.MyInterceptor "></interceptor>
		</interceptors>
		
		<action name=" test "class=" Com.bjsxt.action.TestAction ">
			<result>/test.jsp</result>
			<interceptor-ref name=" my " ></interceptor-ref>
			<interceptor-ref name= "Defaultstack" ></interceptor-ref>
		</ action>
	</package>
</struts>

2.3 Test Run

After deploying to Tomcat, call test.action through the browser, and you can see that the program is printed in the background:



Complete the full text. Reprint please indicate the source.



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.