struts2实现简单拦截器

           struts2实现拦截器的方法有三种:

     1:实现Interceptor接口,需要实现init(),destroy(),intercept(ActionInvocation actionInvocation)三个方法。

     2:继承AbstractInterceptor,只需要重写intercept(ActionInvocation actionInvocation)方法,他帮我们实现了 空的 init()和destroy()方法。

    3:实现MethodFilterInterceptor接口,实现doIntercept(ActionInvocation a)方法,他可以对指定action 的某个方法进行拦截。

    

		<!-- 定义方法拦截器 -->
			<interceptor name="method1" class="com.interceptor.MethodInterceptor1"> 
			    <!-- includeMethods用于指定需要拦截的方法,excludeMethods用于指定不需要拦截的方法--> 
			    <!-- 如果没有提供includeMethods,那么所有的方法都会被拦截 -->  
                   <param name="includeMethods">execute</param>    
            </interceptor>    
      includeMethods用于指定哪些方法需要被拦截。

      excludeMethods用于指定哪些方法不需要被拦截。但是action中的方法体总是会被执行的。

     如果没有提供includeMethods,那么所有的方法都会被拦截。

     对多个方法进行拦截的话 方法名1,方法名2 之间用,隔开

     

  <param name="includeMethods">execute,myexecute</param>   

      现在先实现第一种方法:

      1:实现Interceptor接口

      

package com.interceptor;

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

public class Interceptor1 implements Interceptor{

	public void destroy() {
	
		
	}

	public void init() {
	
		
	}

	public String intercept(ActionInvocation actionInvocation) throws Exception {
		
	    System.out.println("我是自定义拦截器,开始拦截前的处理");
	    
	    String result = actionInvocation.invoke();
	    
	    System.out.println("我是自定义拦截器,开始拦截后的处理");
	    
		return result;
	}

}

   2:在struts2中定义拦截器

     

		<!-- 定义一系列拦截器 -->
		<interceptors>
			<!-- 定义拦截器 -->
			<interceptor name="interceptor1" class="com.interceptor.Interceptor1">
			</interceptor>
		</interceptors>

   3:引用拦截器

   如果我们的action有自己定义引用拦截器后,action就不会用户默认拦截器栈,则有些功能就实现不了。因为定义我们的引用拦截器后,最好也把默认的拦截器栈加上。

     现在我们先定义一个拦截器栈。然后action引用我们的拦截器栈。

    

	<!-- 定义一系列拦截器 -->
		<interceptors>
		
			<!-- 定义拦截器 -->
			<interceptor name="interceptor1" class="com.interceptor.Interceptor1">
			</interceptor>
			
			<!-- 定义拦截器栈,引用了 interceptor1 和 defaultStack -->
			<interceptor-stack name="myStack">
				<interceptor-ref name="interceptor1"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		
		</interceptors>

   action引用拦截器栈:

  

	<action name="Login" class="com.action.LoginAction">
			<!-- 引用拦截器 -->
			<interceptor-ref name="myStack"></interceptor-ref>
			<result name="success">/weclome.jsp</result>
			<result name="failed">/index.jsp</result>
			<!-- 验证出错后跳转的页面 -->
			<result name="input">/index.jsp</result>
		</action>

   完整的struts2.xml

  

<?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>

	<package name="default" extends="struts-default">
	
		<!-- 定义一系列拦截器 -->
		<interceptors>
		
			<!-- 定义拦截器 -->
			<interceptor name="interceptor1" class="com.interceptor.Interceptor1">
			</interceptor>
			
			<!-- 定义拦截器栈,引用了 interceptor1 和 defaultStack -->
			<interceptor-stack name="myStack">
				<interceptor-ref name="interceptor1"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		
		</interceptors>

		<action name="Login" class="com.action.LoginAction">
			<!-- 引用拦截器 -->
			<interceptor-ref name="myStack"></interceptor-ref>
			<result name="success">/weclome.jsp</result>
			<result name="failed">/index.jsp</result>
			<!-- 验证出错后跳转的页面 -->
			<result name="input">/index.jsp</result>
		</action>
		
	</package>



</struts>

   现在拦截器就可以开始工作了。



    2:继承AbstractInterceptor,这个和上面的方法差不多一样,就不多说了。



    3:方法拦截器

      一:继承MethodFilterInterceptor类:

    

package com.interceptor;

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

public class MethodInterceptor1 extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation a)
	         throws Exception {
		
		 System.out.println("方法拦截器前置处理");
		 String result = a.invoke();
		 System.out.println("方法拦截器后续处理");
		 return result;
	}

}

  二:配置拦截器

   

	<!-- 定义一系列拦截器 -->
		<interceptors>
			<!-- 定义方法拦截器 -->
			<interceptor name="method1" class="com.interceptor.MethodInterceptor1"> 
			    <!-- includeMethods用于指定需要拦截的方法,excludeMethods用于指定不需要拦截的方法--> 
			    <!-- 如果没有提供includeMethods,那么所有的方法都会被拦截 -->  
                   <param name="includeMethods">execute,myexecute</param>    
            </interceptor>    
		</interceptors>

    引用拦截器

    

		<action name="Login" class="com.action.LoginAction">
			<!-- 引用拦截器 -->
			<interceptor-ref name="method1"></interceptor-ref>
			<result name="success">/weclome.jsp</result>
			<result name="failed">/index.jsp</result>
			<!-- 验证出错后跳转的页面 -->
			<result name="input">/index.jsp</result>
		</action>

     完整的struts2.xml


      

<?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>

	<package name="default" extends="struts-default">
	
		<!-- 定义一系列拦截器 -->
		<interceptors>
			<!-- 定义方法拦截器 -->
			<interceptor name="method1" class="com.interceptor.MethodInterceptor1"> 
			    <!-- includeMethods用于指定需要拦截的方法,excludeMethods用于指定不需要拦截的方法--> 
			    <!-- 如果没有提供includeMethods,那么所有的方法都会被拦截 -->  
                   <param name="includeMethods">execute,myexecute</param>    
            </interceptor>    
		</interceptors>

		<action name="Login" class="com.action.LoginAction">
			<!-- 引用拦截器 -->
			<interceptor-ref name="method1"></interceptor-ref>
			<result name="success">/weclome.jsp</result>
			<result name="failed">/index.jsp</result>
			<!-- 验证出错后跳转的页面 -->
			<result name="input">/index.jsp</result>
		</action>
		
	</package>



</struts>


    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值