In my personal summary, the Struts2 interceptor uses and intercepts stack configurations. Based on the annotation method, struts2 intercepts

Source: Internet
Author: User

In my personal summary, the Struts2 interceptor uses and intercepts stack configurations. Based on the annotation method, struts2 intercepts

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:

<Package name = "default" namespace = "/" extends = "struts-default"> <-- All configurations should be placed in the package --> <interceptors> <-- all interceptors custom configurations, references, and definitions of intercepted stacks should all be placed in interceptors --> <-- custom interceptor name specifies the reference name of the custom interceptor class specifies the implementation class of the Interceptor (full path) --> <interceptor name = "sessionCheck" class = "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 global default Action --> <default-action-ref name = "IndexAction"/> <-- configure global default result --> <global-results> <-- configure 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" class = "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. Otherwise, reference --> <result name = "success"> Success. jsp </result> <result name = "input"> input. jsp </result> </action> </package>

 

 

Use interceptor and interception stack in annoction Annotation

// Write directly at the top of the class name, and specify the name of the Interceptor to be introduced in value as @ InterceptorRef (value = "token") // reference the interception stack, the blue font indicates the reference name of the interception stack @ 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.

 


Sequence of struts2 custom interceptor and default interceptor Stack

<Interceptor-ref name = "myInterceptor"> </interceptor-ref>
<Interceptor-ref name = "defaultStack"> </interceptor-ref>

<Interceptor-ref name = "defaultStack"> </interceptor-ref>
<Interceptor-ref name = "myInterceptor3"> </interceptor-ref>

What happened to myInterceptor3. Wrong.
The default interceptor has nothing to do with the sequence of the custom interceptor. Generally, the write and send tasks are pre-defined, and the default value is post. I usually write it like this.

The method specified in the interception action of the struts2 interceptor can be

1. inherit from MethodFilterInterceptor (this class is a subclass of AbstractInterceptor)

Import java. util. Map;

Import com. opensymphony. xwork2.Action;
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionInvocation;
Import com. opensymphony. xwork2.interceptor. MethodFilterInterceptor;

/*
* Intercept the specified Method
*/
Public class MyFilterInterceptor extends MethodFilterInterceptor {
Private static final long serialVersionUID = 1L;
Private String name;
Public void setName (String name)
{
This. name = name;
}
@ Override
Protected String doIntercept (ActionInvocation invocation) throws Exception {
// Get the ActionContext instance related to the request
ActionContext ctx = invocation. getInvocationContext ();
Map session = ctx. getSession ();
// Retrieve the Session attribute named user
String user = (String) session. get ("user ");
// If you have not logged in, or the user name used for logging in is not scott, return to re-Login
If (user! = Null & user. equals ("scott "))
{
Return invocation. invoke ();
}
// If you have not logged on, set the server prompt to an HttpServletRequest attribute.
Ctx. put ("tip", "You have not logged on yet. Please enter scott and tiger to log on to the system ");
// Directly return the logic view of login
Return Action. LOGIN;
}

}

2. struts. xml configuration

<Package name = "site" extends = "struts-default" namespace = "/site">
<Interceptors>
<! -- Defines an interceptor named authority -->
<Interceptor name = "authority" class = "cn. zgcyx. filter. MyFilterInterceptor"/> <! -- The Custom interceptor class above -->
<Interceptor-stack name = "my ...... the remaining full text>

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.