【Web开发】Struts2中拦截器的使用

拦截器的作用

1.用于判断用户是否登录。
2.用于过滤用户输入中的非法字符。

配置拦截器

web.xml为Struts2的基本xml,见上一篇文章

Struts.xml:

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

<struts>
    <include file="struts-default.xml"/>
    <package name="testInterceptor" extends="struts-default">
        <interceptors>
            <!-- 定义一个拦截器 -->
            <interceptor name="check" class="interceptor.LoginInterceptor"></interceptor>
            <!-- 定义一个拦截器栈 -->
            <interceptor-stack name="checkLogin">
                  <interceptor-ref name="check"></interceptor-ref>
                  <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <!-- 设置为默认拦截器,即对所有页面进行拦截 -->
        <default-interceptor-ref name="checkLogin"></default-interceptor-ref>
        <action name="checkLogin" class="act.LoginAction" method="checkLogin">
            <result name="success">/index.jsp</result>
            <result name="login">/login.jsp</result>
        </action>
        <action name="TestAction" class="act.TestLogin">
           <result name="success">/index.jsp</result>  
           <result name="login">/login.jsp</result>    
        </action>
    </package>
</struts>

如果是单独的给某个action进行过滤,则通过以下方式:

<action name="checkLogin" class="act.LoginAction" method="checkLogin">
     <result name="success">/index.jsp</result>
     <result name="login">/login.jsp</result>
     <interceptor-ref name="check"></interceptor-ref>
     <interceptor-ref name="defaultStack"></interceptor-ref>
</action>

注意:由于对所有页面过滤包括登录界面,所以要在拦截器代码中设置一下

package interceptor;

import java.util.Map;

import act.LoginAction;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor{

    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        Map session = arg0.getInvocationContext().getSession();
        String username=(String)session.get("username");
        if(LoginAction.class==arg0.getAction().getClass()){//如果是登录Action就继续下一个过滤器或执行Action的方法
            return arg0.invoke();
        }
        if(username!=null&&username.length()>0){//如果登录则继续,否则返回到login(和success类似)
            return arg0.invoke();
        }else{
            ActionContext ac=arg0.getInvocationContext();
            ac.put("popedom","您还没有登录,请登录!");
            return Action.LOGIN;
        }
    }
}

方法过滤拦截器

MethodIntercept .java

package interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class MethodIntercept extends MethodFilterInterceptor{

    @Override
    protected String doIntercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        //setExcludeMethods("execute"); 这样设置后会覆盖掉parm的默认值
        System.out.println("拦截器起作用了!");
        return arg0.invoke();
    }
}

struts.xml

<interceptor name="method" class="interceptor.MethodIntercept">
    <!-- 设置不需要被拦截的方法 -->
    <param name="excludeMethods">execute1,execute2</param>    
    <!-- 设置需要被拦截的方法 -->
    <param name="includeMethods">execute1,execute3</param>  
    <!-- 如果冲突时会被拦截,即execute1会被拦截,与exclude和include顺序无关 -->           
</interceptor>

拦截器顺序

先执行先定义的拦截器,invoke()之后等后面的拦截器包括action执行完后再执行,类似于栈,先入后出

拦截器参数

后定义的参数覆盖定义时的参数
当在action中需要改变过滤器栈中的参数时:
例如过滤器栈<1,2,3,4>,需要改变1的参数值name,则输入:

<action name="checkLogin" class="act.LoginAction" method="checkLogin">
     <result name="success">/index.jsp</result>
     <result name="login">/login.jsp</result>
     <interceptor-ref name="stack">
         <param name="1.name">参数值</param>
     </interceptor-ref>
</action>

猜你喜欢

转载自blog.csdn.net/tjj1998/article/details/80525670