The Struts2 interceptor can be used to intercept the struts2 interceptor.

Source: Internet
Author: User

The Struts2 interceptor can be used to intercept the struts2 interceptor.

I am studying the struts interceptor recently. Let's summarize it now.

1. What is an interceptor?

The Interceptor is equivalent to a filter: It removes unwanted ones and leaves them. The interceptor abstracts a part of the code to improve the original action. At the same time, it can reduce code redundancy and improve the reuse rate. In general, it is a network that filters out unwanted sand and leaves water.

2. Role of interceptor:

The interceptor can constitute a specific function. For example, permission authentication, logging, and logon judgment.

3. interceptor principle:

  

Each of its Action requests is in the Interceptor. Each action can forward the operation to the interceptor below, or directly exit the interface.

4. Define the Interceptor:

(1) customize an Interceptor interface (however, I am a beginner who can directly implement Interceptor in the Framework)

(2) register the defined interceptor in struts. xml

(3) the interceptor can be referenced in the required action.

The Interceptor interface declares three methods.

  

1 public interface Interceptor extends Serializable {2  3     void destroy();4  5     void init();6  7     String intercept(ActionInvocation invocation) throws Exception;8 }

The Init method is called before the action, that is, the initialization operation for the rotten machine is started.

The Destory method is called before the interceptor is reclaimed by garbage collection to recycle the resources initialized by the init method.

The interceptor method is the main operation of the interceptor. To call subsequent actions or interceptors, you only need to call invocation in this method. the invoke () method can be used to insert the methods required by the interceptor before and after the Action call.

The following code intercepts User Logon:

Public String intercept (ActionInvocation invocation) throws Exception {System. out. println ("before the action is executed"); ActionContext actionContext = invocation. getInvocationContext (); Map <String, Object> session = actionContext. getSession (); Object currentUser = session. get ("currentUser"); String result = null; if (currentUser! = Null) {result = invocation. invoke ();} else {HttpServletRequest request = (HttpServletRequest) invocation. getInvocationContext (). get (ServletActionContext. HTTP_REQUEST); request. setAttribute ("error", "Log on first"); result = "error";} System. out. println ("result +" + result); System. out. println ("after the action is executed"); return result ;}

Register the Interceptor:

<interceptors>             <interceptor name="myInterceptor"             class="com.fangchao.interceptor.MyInterceptor"></interceptor>             <interceptor name="loginInterceptor"             class="com.fangchao.interceptor.LoginInterceptor"></interceptor>                          <interceptor-stack name="myStack">                 <interceptor-ref name="loginInterceptor"></interceptor-ref>                 <interceptor-ref name="defaultStack"></interceptor-ref>                 </interceptor-stack>         </interceptors>

In the above Code, interceptor-stack is an interceptor stack. So far, it is more convenient to reference it below. Generally, defaultStack is used for each action.

Interceptor parameters:

Configuration parameters:

ExcludeMethods: filter out methods that do not use interceptor

IncludeMethods: the method that uses the interceptor.

There are two configuration methods:

[1]

<Interceptor-ref name = "validation"> <param name = "excludeMethods"> myValidationExcudeMethod </param> </interceptor-ref> <interceptor-ref name = "workflow"> <param name = "excludeMethods"> myWorkflowExcludeMethod </param> </interceptor-ref> or <interceptor-ref name = "defaultStack"> <param name = "validation. excludeMethods "> myValidationExcludeMethod </param> <param name =" workflow. excludeMethods "> myWorkflowExcludeMethod </param> </interceptor-ref>

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.