struts2--interceptor

Interceptors of struts:
1. Process: To
  access the action, first go to the filter and distribute it to different actions. Execute a series of interceptors in the filter;
2. The difference between filter and interceptor:
 filter: to Filter before the target resource, you can filter all content (action, jsp, html); interceptor: intercept before the target resource, only the action can be intercepted;
3. The use principle of the interceptor:
 aop: Aspect-oriented programming, the bottom layer adopts dynamic Proxy method
 Chain of responsibility mode: there is a set of operations, tied on a line, when an operation is completed, go to the next operation
4. Interceptor execution process:
 When accessing an action, first go to the StrutsPrepareAndExecuteFilter filter
 [in this class There are init method: used to load configuration files; doFilter method: execute the default interceptor],
 execute [executeAction method execution interceptor] multiple interceptors in the doFilter method in the filter,
 and generate the proxy object of the current action [
  ActionProxy] proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, method, extraContext, true, false);
 ], when the interceptor is executed, execute the invoke method and execute the next interceptor
5, The structure of the interceptor:
  The interceptor inherits the AbstractInterceptor class, which implements the Interceptor interface
  . There are three life cycle methods in the class,
  init intercept destroy
  . The interception logic is written in the intercept method.

Custom interceptor:
1), create a class that inherits the AbstractInterceptor class
2), rewrite the intercept method, and write the interception logic in this method
3), register a custom interceptor
/*
  * Determine whether it is a login?
  * = Determine whether there is a user object in the session, if there is a login, there is no login
 */
 public String intercept(ActionInvocation invocation) throws Exception {
  //It is recommended to use the actionContext method to operate
  ActionContext context = invocation.getInvocationContext();
  // Get the value in the session
  Object object = context.getSession().get("user");
  //Determine whether it is empty
  if(object != null) { //Already logged
   in //Execute the next operation
   return invocation.invoke( );
  } else {//No login
   return Action.LOGIN;
  }
 }
 
Register the interceptor:
declare the interceptor:
 (1) Declare a custom interceptor in the package where the action is located
  <interceptors>
   <interceptor name="loginInterceptor" class="cn.xxx.demo4.MyInterceptor"></interceptor>
  </interceptors>
Use interceptors:
 (2) Use the declared interceptor in action
  <interceptor-ref name ="loginInterceptor"></interceptor-ref>
 (3) Use a custom interceptor, the default inherited interceptor will be invalid, and the declaration that needs to be displayed will be displayed in the
  <interceptor-ref name="defaultStack"></interceptor-ref>
section. Two registration methods:
declare the interceptor:
 (1) declare the interceptor in the package where the action is located, define the stack, and write the introduction of the interceptor in the stack
  <interceptors>
   <interceptor name="loginInterceptor" class="cn.xxx. demo4.MyInterceptor"></interceptor>
   <interceptor-stack name="myStackLogin">
    <interceptor-ref name="defaultStack"></interceptor-ref>[***]
    <interceptor-ref name="loginInterceptor"></interceptor-ref>
   </interceptor-stack>
  </interceptors>
Use interceptors:
  (2) You can use the defined stack directly in the action
  <interceptor-ref name="myStackLogin"></interceptor-ref>  

The difference between addFieldError and addActionError
 (1) addFieldError refers to form input data is incorrect
 (2) addActionError refers to business problems

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326538608&siteId=291194637