"Original" Personal summary of Java Ultra-detailed Struts2 interceptor usage and interception stack configuration

Source: Internet
Author: User

Struts2 's interceptor is a good weapon! In particular, custom interceptors, the following is my personal collation of the entire process, I hope to bring you help, there is a better view of the hope that can provide valuable advice.

Principle:

A) The Web browser sends the request

b) First through a set of Struts2 default interception stacks dispatcher (or servletfilter)

c) Custom Interceptor (Interceptor)

D) Action

e) Result

The Struts.xml configuration is as follows:

< Packagename= "Default"namespace= "/"extends= "Struts-default" ><--All configurations should be placed in the package-->    <Interceptors>        <-- All interceptor custom configurations and references, and the definition of blocked stacks should be placed in the interceptors->        <-- Custom Interceptor Name specifies the reference name of the custom interceptor class specifies the implementation class for the Interceptor (full path)-->        <Interceptorname= "Sessioncheck"class= "Com.cqrcb.perinvite.interceptor.AuthorityInterceptor" />                 <-- Custom Block Stack name specifies the reference name of the custom interception stack-->                    <Interceptor-stackname= "Sessioncheckstack">                        <-- The reference name for the interceptor to be referenced has just defined an interceptor named Sessioncheck, and the reference is sessioncheck-->                         <-- each custom intercept stack should be equipped with the Defaultstack intercept stack, which is the STRUTS2 default interception stack, which encapsulates a set of interceptors-> <Interceptor-refname= "Defaultstack" />                        <Interceptor-refname= "Sessioncheck" />        </Interceptor-stack>    </Interceptors>    < --  Configure the global Default action-->    <Default-action-refname= "Indexaction" />         < -- Configuring the global default result-->                <Global-results>                    <-- Configure the Invalid.token return view of token, that is, when the page repeats, the page will automatically go to the/error.jsp page and prompt-->                    <resultname= "Invalid.token">/error.jsp</result>                    <resultname= "Error">/error.jsp</result>                                        <-- Configure Resule with name Backhome, redirect to indexaction-->                    <resultname= "Backhome"type= "Redirectaction">Indexaction</result>                    <-- Configure the Resule for name Testchain, forward to testaction-->                    <resultname= "Testchain"type= "Chain">Testaction</result>    </Global-results>    < --There are two ways to use interceptors, annoction (annotations) and XML configuration, the following is the XML configuration-->                <Actionname= "Testaction"class= "Com.cqrcb.perinvite.resume.action.testAction">                    <-- introduce the Sessioncheckstack intercept stack name to the reference name defined for the interception stack before this action access-->                    <-- This sessioncheckstack already contains the default interceptor stack for custom interceptors and Struts2, so you can refer to Sessioncheckstack directly-->                    <Interceptor-refname= "Sessioncheckstack"/>                                        <-- If you refer to a custom interceptor directly, which does not contain the default interception stack, you need to refer to the STRUTS2 default intercept stack, the following-->                    <Interceptor-refname= "Testinter"/><interceptor-ref name= "Defaultstack"/><--an action as long as there is a defaultstack, if the reference to the interception stack has defaultstack, you do not have to refer to Defaultstack, otherwise, cited--><resultname= "Success">success.jsp</result>                        <resultname= "Input">input.jsp</result>    </Action></ Package>

Using interceptors and interception stacks in annoction annotations

// write directly to the upper end of the class name, specifying the name of the interceptor to be introduced in value @InterceptorRef (value= "token")// intercept stack reference, The blue font is the reference name of the interception stack @InterceptorRefs (@InterceptorRef ("Sessioncheckstack"))



JavaBean of Custom Interceptors

 PackageCom.cqrcb.perinvite.interceptor;Importcom.cqrcb.perinvite.logon.action.IndexAction;ImportCom.netbank.pub.vo.core.PbClientInfoVO;ImportCom.opensymphony.xwork2.ActionContext;Importcom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.AbstractInterceptor;/*** permission to intercept action *@authorWangyupeng **///inheriting the Abstractinterceptor class Public classAuthorityinterceptorextendsabstractinterceptor{Private Static Final LongSerialversionuid = 4546936882066035745L; //overriding the Intercept method     PublicString Intercept (actioninvocation invocation)throwsException {//get the action's intercept pathActioncontext ax =Invocation.getinvocationcontext (); //Get Action ObjectObject action =invocation.getaction (); //Indexaction does not do this intercept action instanceof Indexaction interpreted as if an instance of action is Indexaction        if(Actioninstanceofindexaction) {              //if it is indexaction, execute it, not intercept .            returnInvocation.invoke (); }         //gets the object of key pinfo in sessionPbclientinfovo pinfo = (Pbclientinfovo) ax.getsession (). Get ("pinfo"); if(pinfo==NULL){            //if pinfo is null, the view that the global result is Backhone is returned            return"Backhome"; }        //If neither is false, no interception is done        returnInvocation.invoke (); }       }

There are many ways to struts2 a custom interceptor, and the content of this article is a pre-intercept, which is the interception of the request that gets to the action. Struts2 also have back-intercept and intermediate interception, these two are also very common, have time to finish after finishing I will send to the blog, we discuss together.

"Original" Personal summary of Java Ultra-detailed Struts2 interceptor usage and interception stack configuration

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.