I personally summarized the detailed Struts2 interceptor usage and interception stack configuration in java.

Source: Internet
Author: User

Struts2 interceptor is a good tool! In particular, the custom Interceptor. The following is a detailed process that I personally organized. I hope to help you and provide valuable opinions.

 

Principle:

A) The Web browser sends a request

B) First, use a set of Struts2 default interception stack dispatcher (or ServletFilter)

C) custom interceptor (interceptor)

D) Action

E) Result

 

The struts. xml configuration is as follows:

<Name = "" namespace = "/" = "struts-"> <-- All configurations should be placed in the package --> <interceptors> <-- custom configurations of all interceptor and references and definitions of intercepted stacks should be placed in interceptors --> <-- custom interceptor name specifies the reference name of the custom interceptor and specifies the implementation class of the Interceptor (full path) --> <interceptor name = "sessionCheck" = "com. cqrcb. perinvite. interceptor. authorityInterceptor "/> <-- custom interception stack name specifies the reference name of the custom interception stack --> <interceptor-stack name =" sessionCheckStack "> <-- name indicates the name of the interceptor to be referenced the reference name just defined an interceptor named sessionCheck, the reference is sessionCheck --> <-- each custom interception stack must be matched with the defaultStack interception stack, which is the default interception stack of Struts2, it encapsulates a group of interceptors --> <interceptor-ref name = "defaultStack"/> <interceptor-ref name = "sessionCheck"/> </interceptor-stack> </interceptors> <-- configure the global default Action --> <-action-ref name = "IndexAction"/> <-- configure the global default result --> <global-results> <-- configure the token invalid. token return view. When the page is submitted repeatedly, the page is automatically forwarded to/error. the jsp page prompts --> <result name = "invalid. token ">/error. jsp </result> <result name = "error">/error. jsp </result> <-- configure resule whose name is backhome, redirect to IndexAction --> <result name = "backhome" type = "redirectAction"> IndexAction </result> <-- configure resule whose name is testChain, jump to testAction --> <result name = "testChain" type = "chain"> testAction </result> </global-results> <-- there are two ways to use the interceptor, annoction (annotation) and xml configuration. The following is XML configuration --> <action name = "testAction" = "com. cqrcb. perinvite. resume. action. testAction "> <-- Introduce sessionCheckStack interception stack name as the reference name defined by the interception stack before this Action access --> <-- This sessionCheckStack already contains the default interception stack of the custom interceptor and Struts2, therefore, you can directly reference sessioncheckstack --> <interceptor-ref name = "sessionCheckStack"/> <-- If you directly reference a custom interceptor, that is, it does not contain the default interception stack, you need to reference Struts2's default interception stack, <interceptor-ref name = "testInter"/> <interceptor-ref name = "defaultStack"/> <-- only one defaultStack exists in an Action, if the referenced interception Stack has defaultStack, you do not need to reference defaultStack any more. Otherwise, reference --> <result name = "success"> </result> <result name = "input"> </result> </action> </>

 

Use interceptor and interception stack in annoction Annotation

      @InterceptorRef(value="token"      @InterceptorRefs(@InterceptorRef("sessionCheckStack"))

 

Javabean of the custom interceptor

Package com. cqrcb. perinvite. interceptor; import com. cqrcb. perinvite. logon. action. indexAction; import com. netbank. pub. vo. core. pbClientInfoVO; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor;/*** permission interception Action * @ author wangyupeng ** // inherits the AbstractInterceptor class public class AuthorityInterceptor extends actinterceptor {private static final long serialVersionUID = response; // override the intercept method public String intercept (ActionInvocation invocation) throws Exception {// obtain the interception path of the Action ActionContext ax = invocation. getInvocationContext (); // get the Action Object action = invocation. getAction (); // The action instanceof IndexAction is interpreted as if the Action instance is IndexAction if (action instanceof IndexAction) {// if it is IndexAction, it is executed, return invocation is not intercepted. invoke () ;}// obtain the object PbClientInfoVO pinfo = (PbClientInfoVO) ax whose key is pinfo in the session. getSession (). get ("pinfo"); if (pinfo = null) {// if pinfo is null, return the view with the global result of backhone return "backhome ";} // if neither of them is false, return invocation is not blocked. invoke ();}}

  

 

The custom interceptor of struts2 has many methods. The content in this article is pre-interception, that is, the interception of the request to obtain the action. Struts2 also has post-interception and intermediate interception, which are also very common. I will post it to my blog after I have time to sort it out. Let's discuss it together.

Related Article

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.