The use of interceptor in Struts2

Interceptor (Interceptor) mainly completes the analysis of request parameters, assigns page form parameters to corresponding attributes in the value stack, performs function checks, and debugs abnormal programs. Chinese garbled characters are prone to problems. Each Action request is wrapped in a series of interceptors. The interceptor can do similar operations before the Action is executed, or it can do the recycling operation after the Action is executed. Action can transfer the operation to the interceptor below, or exit the operation directly and return to the established page. What I am going to talk about is user login check.
1. First define a class to implement the com.opensymphony.xwork2.interceptor.Interceptor interface, sample code:

package com.gx.intercepter;

import java.util.Map;

import com.gx.po.User;
import com.gx.web.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginIntercepter implements Interceptor{
    
    

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    //Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源
    @Override
    public void destroy() {
    
    
        System.out.println("LoginIntercepter 回收");
    }

    @Override
    public void init() {
    
    
        System.out.println("LoginIntercepter 初始化");

    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
    
    
        //排除登录Action
        Object action= actionInvocation.getAction();//获取action
        if (action instanceof LoginAction) {
    
    
            System.out.println("LoginAction 退出拦截器");
            return actionInvocation.invoke();
        }

        //获取session
        Map<String, Object> session= actionInvocation.getInvocationContext().getSession();
        User pfUser=(User) session.get("pfUser");
        if (pfUser!=null && pfUser.getUserName().length()>0) {
    
    
            System.out.println("已经登录,退出拦截器");
            return actionInvocation.invoke();
        }else{
    
    
            System.out.println("没有登录,跳转到登录页面");
            return "login";//对应全局的返回变量
        }
    }
}

2. Interceptor configuration in strurs2.xml, set the interceptor to be global. It should be noted that the interceptor must be defined before being inherited, and don't forget to add the default interceptor stack, otherwise some functions of Struts2 will not work properly. Sample code:

<package name="base-package" extends="struts-default">
	<interceptors>
		<!-- 声明自定义拦截器 -->
		<interceptor name="loginIntercepter" class="com.gx.intercepter.LoginIntercepter">
		</interceptor>
		<!-- 配置拦截栈 -->
		<interceptor-stack name="myStack">
			<!-- 添加自定义的拦截器 -->
			<interceptor-ref name="loginIntercepter"></interceptor-ref>
			<!-- 添加默认的拦截器栈 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</interceptor-stack>
	</interceptors>
	
	<!-- 设置当前 namespace的默认拦截器栈 -->
	<default-interceptor-ref name="myStack"></default-interceptor-ref>
	<!-- 配置全局的返回 -->
	<global-results>
		<result name="login" type="redirect">/jsp/intercepter/login.jsp</result>
	</global-results>
</package>

<package name="intercepterDemo" namespace="/intercepterDemo" extends="base-package">
	<action name="loginAction" class="com.gx.web.LoginAction">
		<result name="success" type="redirectAction">
			<param name="namespace">/intercepterDemo</param>
			<param name="actionName">testAction</param>
			<param name="method">index</param>
		</result>
		<result name="error">/jsp/fail.jsp</result>
	</action>
	<action name="testAction" class="com.gx.web.TestAction" method="index">
		<result name="success">/jsp/intercepter/test.jsp</result>
	</action>
</package>

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/106836896