Struts2自定义拦截器

 

实现或继承类

struts2系统自带了很多拦截器,有时需要我们自己定义,一般有三种方式:

一、实现Interceptor接口

public interface Interceptor extends Serializable{  
     public void init();  
     public void destroy();  
     public String intercept(ActionInvocation invocation)();  
}  

二、继承AbstractInterceptor类,重写intercept()方法即可 (一般用此种)

 其实AbstractInterceptor类也就是实现了Interceptor接口

invocation.invoke();表示该方法执行完后执行Action的execute()方法或者执行下一个拦截器  
invocation.getAction(); 可以将该法强制转换为Action的类类型  

 

例子:

package com.ywjava.interceptot;

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.ywjava.utils.Constants;

public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		// 取得请求相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		String user = (String) session.get(Constants.USER_SESSION);

		// 如果没有登陆,或者登陆所有的用户名不是yuewei,都返回重新登陆

		if (user != null && user.equals("yuewei")) {
			System.out.println("test");
			return invocation.invoke();
		}

		ctx.put("tip", "你还没有登录");
		return Action.LOGIN;

	}

}

 


三、方法拦截器:继承MethodFilterInterceptor类,重写doIntercept()方法

       MethodFilerInterceptor实现方法过滤中用到的两个参数

execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,拦截器不会拦截指定列表中的方法,就是所谓的黑名单

includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,即白名单。

 

struts.xml文档中配置自定义拦截器

使用上面的三种方法,定义好自定义拦截器后,就要使用自定义拦截器,在struts.xml文档中

一、包内定义拦截器

<package name="default" namespace="/test" extends="struts-default">

	<interceptors>
		<interceptor name="permission" class="com.qn.intercepter.PermissionIntercepter"></interceptor>
		<interceptor-stack name="mystack">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<!-- 定义拦截器栈 <interceptor-ref name="defaultStack"></interceptor-ref>要放在第一行	 -->
			<interceptor-ref name="permission"></interceptor-ref>
		</interceptor-stack>
	</interceptors>

	<global-results>
		<result name="error">/error.jsp</result>
	</global-results>

	<action name="list_*" class="com.qn.struts.HellowAction" method="{1}">
		<interceptor-ref name="mystack"></interceptor-ref> 	 
		<result name="success">success.jsp</result>
	</action>

</package>
</struts>

二、action内使用拦截器

<action .....>  
     <result.....></result>  
     <interceptor-ref name="defaultStack"></interceptor-ref>  
     <interceptor-ref name="myinterceptor"></interceptor-ref>  
</action>  


 下面是一个例子

MyMethodInterceptor.java

public class MyMethodInterceptor extends MethodFilterInterceptor{  
    protected String doIntercept(ActionInvocation invocation) throws Exception {  
       
        System.out.println("进入MyMethodInterceptor方法拦截器!!!!!!!!!!!!!");  
        Map session = invocation.getInvocationContext().getSession();  
        String name = (String) session.get("uname");  
        if (name != null) {  
            return invocation.invoke();  
        }  
        return "input";  
    }  
} 

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>  
<constant name="struts.i18n.encoding" value="UTF-8"></constant>  
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  
<!-- <constant name="struts.action.extension" value="action,abc"></constant>-->  
      
    <package abstract="true" name="pk1" namespace="/" extends="struts-default"></package>  
      
    <package name="pk2" extends="pk1">  
        <interceptors>  
            <interceptor name="test1" class="org.interceptors.MyTimerInterceptor"/>               
            <interceptor name="method1" class="org.interceptors.MyMethodInterceptor">  
                <param name="excludeMethods">login</param>  
            </interceptor>

            <interceptor-stack name="myStack">  
                <interceptor-ref name="defaultStack"></interceptor-ref>  
                <interceptor-ref name="method1"></interceptor-ref>  
                <interceptor-ref name="test1"></interceptor-ref>                    
            </interceptor-stack>  
			
        </interceptors>  

		<!--   <default-interceptor-ref name="myStack"></default-interceptor-ref>-->  
          
        <action name="login_*_*" class="org.hzy.Actions.LoginAction" method="{1}">  
            <interceptor-ref name="myStack"></interceptor-ref>  
            <result name="success" type="chain">{2}</result>  
            <result name="input">index.jsp</result>  
            <result name="error">/WEB-INF/Jsp/error.jsp</result>  
        </action>  
          
        <action name="query_*" class="org.hzy.Actions.QueryAction" method="{1}">  
            <result>/WEB-INF/Jsp/show.jsp</result>  
        </action>  
          
        <action name="upload_*" class="org.hzy.Actions.FileUploadAction" method="{1}">  
            <result>/WEB-INF/Jsp/show.jsp</result>  
        </action>

    </package>  
</struts>  



 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值