Struts2 interceptor detailed configuration

Struts2 interceptor detailed configuration process

Struts2 uses custom interceptors
  Although the Strtus2 framework provides many interceptors, these built-in interceptors implement most of the functions of Struts2, so most of the common functions of web applications can be used directly by using these Interceptors are used to complete, but there are some general functions related to system logic, which can be implemented by custom interceptors. To its credit, Struts2's interceptor system is so simple and easy to use.
One: Implementing the interceptor
If the programmer wants to develop his own interceptor class, he should implement the com.opensymphony.xwork2.interceptor interface. The interface code is as follows (struts2 source code):

package com.opensymphony.xwork2.interceptor;

import com.opensymphony. xwork2.ActionInvocation;
import java.io.Serializable;

public abstract interface Interceptor extends Serializable{
  public abstract void destroy();

  public abstract void init();

  public abstract String intercept(ActionInvocation paramActionInvocation)
    throws Exception;
}

This interface defines three method:

1, init(): After the interceptor is initialized and before the interceptor performs the interception, the system will call back this method. The init() method is mainly used to open some resources, such as database resources. This method is executed only once.
2, destroy(): This method corresponds to the init() method. Before the interceptor is destroyed, the system will call back the destroy method of the interceptor, which is used to release the resources opened in the init method.
3, intercept(ActionInvocation paramActionInvocation): This method is for the user to intercept the action. Just like the execute method of Action, the intercept method will return a string as a logical view. If the method directly returns a string, the system will jump to the actual view resource corresponding to the logical view, and will not call the intercepted view. Action. The (ActionInvocation parameter of this method contains a reference to the intercepted action. You can transfer control to the next interceptor by calling the invoke method of this parameter, or to the exctute method of the action.

In addition, Struts2 also provides A com.opensymphony.xwork2.ActionInvocation.AbstractInterceptor abstract class is created, which implements the com.opensymphony.xwork2.interceptor interface, so there is no need to implement the init and destroy methods. The custom interceptor inherits com.opensymphony.xwork2.ActionInvocation.AbstractInterceptor, It will be simpler to implement

2 : The following is a simple interceptor to control login access

/**
*
*/
package com.test.demo.web.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.demo.constants.Constants;
import com.test.demo.model.Userinfo;

/**
* Title: BaseDaoHibernateImpl Description:
*
* Copyright: Copyright (c) 2010 Company: demo
*
* @author zhengzhangwens
* @version 1.0
* @since 2010-01-06
*/
public class UserAuthorityInterceptor extends AbstractInterceptor {

/*
  * (non-Javadoc)
  *
  * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com
  * .opensymphony.xwork2.ActionInvocation)
  */
@Override
public String intercept(ActionInvocation invocation) throws Exception {
  // Get the ActionContext instance related to the request
  ActionContext ctx = invocation .getInvocationContext();
  Map session = ctx.getSession();
  // Take out the session attribute named user Userinfo
  user = (Userinfo) session.get(Constants.SESSION_USER);
  // If not logged in, return to log in again
  if (user != null) {
   return invocation.invoke();
  }
  // No login, set the server prompt to an HttpServletRequest attribute
  ctx.put("tip", "You are not logged in, please log in to the system");
  return Action.LOGIN ;
}

}

 
Three: Configure the interceptor in the struts.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.devModel" value="true" />

<!-- System Management -->
< package name="system" extends="struts-default"
  namespace="/system">
  <!-- add by zhengzhangwen 2011-02-14 Add management background global exception interception and background login verification interception -->
  <!-- Configure the interceptors required by the application-->
  <interceptors>
   <!-- Global exception interception-->
   <interceptor name=" exceptionInterceptor"
    class="com.test.demo.web.interceptor.ExceptionInterceptor">
   </interceptor>
   <!-- Background login verification interception-->
   <interceptor name="userAuthorityInterceptor"
    class="com.test.demo.web.interceptor.UserAuthorityInterceptor">
   </interceptor>
   <!-- Interceptor stack configuration-->
   <interceptor-stack name="systemInterceptorStack">
    <interceptor-ref name="userAuthorityInterceptor" />
    <interceptor-ref name="exceptionInterceptor" />
   </interceptor-stack>
  </interceptors>

  <!-- default interceptor configuration Override the default interceptor in struts2 -->
  <default-interceptor-ref name="systemInterceptorStack"></default-interceptor-ref>

  <!-- global results -->
  <global-results>
   <result name="error">/error.jsp</result>
   <result name="login" type="redirect">/login.jsp</result>
  </global-results>

  <!-- 全局异常 -->
  <global-exception-mappings>
   <exception-mapping exception="java.lang.Exception"
    result="error">
   </exception-mapping>
  </global-exception-mappings>
  <!-- end  -->
 
  <!-- 用户登陆 -->
  <action name="login" class="loginAction" method="login">
   <result name="success">/success.jsp</result>
   <result name="input">/error.jsp</result>
  </action>

  <!-- 用户登出 -->
  <action name="logout" class="loginAction" method="logout">
   <result name="success">/login.jsp</result>
  </action>
</package>


<!-- 办公管理 -->
<package name="official" extends="struts-default"
  namespace="/official">

</package>
</struts>

Guess you like

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