STRUTS2 Tutorial 8: Interceptor Overview

Source: Internet
Author: User

Struts2 interceptors are similar to servlet filters. Before executing the action's Execute method, Struts2 first executes the interceptor referenced in Struts.xml, and executes the action execute method after executing the Intercept method of all the referenced interceptors.

The STRUTS2 interceptor class must inherit from the Com.opensymphony.xwork2.interceptor.Interceptor interface, and there are three methods in the Intercepter interface that need to be implemented:

void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;

Where the Intercept method is the core of the interceptor, all installed interceptors will invoke the method. In Struts2, some self-contained interceptors have been predefined in struts-default.xml, such as timer, params, and so on. If you inherit Struts-default in the <package> tab, the current package automatically owns all the configurations in Struts-default.xml. The code is as follows:

<package name="demo" extends="struts-default" > ... </package>

There is a default reference in Struts-default.xml, and some interceptors are automatically referenced by default (that is, when the interceptor is not referenced in <action>). This default interceptor reference is as follows:

 <default-interceptor-ref name= "Defaultstack"/> <interceptor-stack name= "DefaultStack" > < Interceptor-ref name= "Exception"/> <interceptor-ref name= "Alias"/> <interceptor-ref name= "ServletConfi" G "/> <interceptor-ref name=" Prepare "/> <interceptor-ref name=" i18n "/> <interceptor-ref name= "Chain"/> <interceptor-ref name= "debugging"/> <interceptor-ref name= "profiling"/> <intercept Or-ref name= "Scopedmodeldriven"/> <interceptor-ref name= "Modeldriven"/> <interceptor-ref name= "FILEUPL" Oad "/> <interceptor-ref name=" checkbox/> <interceptor-ref name= "Staticparams"/> R-ref name= "params" > <param name= "excludeparams" >dojo\. *</param> </interceptor-ref> <interceptor-ref name= "Conversionerror"/> <interceptor-ref N Ame= "Validation" > <param name= "excludemethods" >input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name= "Workflow" > <param n Ame= "Excludemethods" >input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack "

The interceptor referenced above in Defaultstack can be used without reference in <action> (if any interceptor is referenced in <action>, the interceptor defined in Defaultstack will be used in the <action>, which will be explained in detail later.

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.