[Struts2 interceptor]

I. Overview

  • Introduction interceptor:
  • struts2 interceptor using AOP (Aspect Oriented Programming) ideas. AOP is the realization of the underlying dynamic proxy. Interceptor uses the chain of responsibility pattern , the chain of responsibility pattern, many objects of each object reference to them at home and connected together to form a chain. The chain of responsibility for each node, you can continue to call the next node, can also block the flow continues. struts2 in struts-default.xmla statement that all of the interceptors file. The struts2 framework is used by default defaultStack this interceptor stack. Use the 18 interceptors in the interceptor stack. Simply put, struts2 framework by default, loaded with 18 interceptors. Access can be controlled action through the use of interceptors. For example, permission to operate

Second, the use of interceptors in Struts2

Step 2.1

  • 1. Create a custom Interceptor can achieve a classcom.opensymphony.xwork2.interceptor.Interceptor

    • There are three methods init destroy intercept, intercept method is a method to intercept true in this interface.
    • If you want to continue down the intercept method, by calling its invoke its parameters ActionInvocation () method can be used.
  • 2. a statement Interceptor

    • Reference struts-default.xmlfile configuration, struts.xml file to declare himself ainterceptor
  • 3. Specify which use interceptors in the action.

    • Note: As long as the statement is displayed using an interceptor. Then the default interceptor is not loaded.

2.2 Analysis interceptor principle

  • Source code execution process:

  • 1. Find StrutsPrepareAndExecuteFilter in a word execute.executeAction (request, response, mapping) Action operations performed within doFilter method.

  • 2. In the process of implementation will visit executeAction Dispatcher class serviceAction, in this method creates a ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false);This is our proxy object Action

  • 3. View ActionInvocation, see its implementation class DefaultActionInvocation in its invoke method:

if (interceptors.hasNext()) {//判断是否有下一个拦截器.
    final InterceptorMapping interceptor = interceptors.next(); //得到一个拦截器
    String interceptorMsg = "interceptor: " + interceptor.getName();
    UtilTimerStack.push(interceptorMsg);
    try {
            resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); 
            //调用得到的拦截器的拦截方法.将本类对象传递到了拦截器中。
        }
    finally {
        UtilTimerStack.pop(interceptorMsg);
    }
} 

  • By source code analysis, it is found in DefaultActionInvocation in operation by the recursive call to complete all of the interception.

2.3 About interceptor and Filter difference:

  • 1, the interceptor java reflection mechanism is based, and the filter is based on the callback function.
  • 2, the filter depends on the servlet container, and does not depend on the interceptors servlet container.
  • 3, the interceptor can only act Action requests, and the filter is almost all acts may request.
  • 4, the interceptor can access Action context, the stack of the value of the object, and the filter can not.
  • 5. Action in the life cycle, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized.

Third, the case

  • Access Control:
  • 1.login.jsp ------> LoginAction -------------> book.jsp, the login is successful, the user will be stored to the session.
  • 2. Provide crud link book.jsp in. Each connector in a method of accessing a BookAction.
  • Requirements: For the BookAction add, update, delete methods require users to log in before you can access. No search.
  • To control the action in some of the methods of interception

  • 1. Create a class does not implement Interceptor, but inherit a subclass .MethodFilterInterceptor under it, without rewriting intercept method, but rewrite doIntercept method.

public class BookInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        // 1.得到session中的user
        User user = (User) ServletActionContext.getRequest().getSession()
                .getAttribute("user");

        if (user == null) {
            BookAction action = (BookAction) invocation.getAction(); // 得到当前拦截的action对象。

            action.addActionError("权限不足,请先登录");// 存储错误信息

            return Action.LOGIN;
        }

        return invocation.invoke();
    }

}
  • 2. In the statement struts.xml file
<interceptors>
    <intercept name="" class="">
        <param name="includeMethods">add,update,delete</param>
        <param name="excludeMethods">search</param>
    </intercept>
</interceptors>

Guess you like

Origin www.cnblogs.com/haoworld/p/struts2-lan-jie-qi.html