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
    评论
Struts2 中,可以使用拦截器实现权限控制。具体来说,可以编写一个自定义的拦截器,然后在 struts.xml 配置文件中将其配置为需要进行权限控制的 Action拦截器。以下是一个简单的权限拦截器示例: ```java public class AuthInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // 获取当前用户 User user = (User) ActionContext.getContext().getSession().get("user"); // 判断用户是否拥有权限 if (user != null && user.hasPermission(invocation.getInvocationContext().getName())) { // 如果有权限,则继续执行 Action return invocation.invoke(); } else { // 如果没有权限,则跳转到错误页面 return "error"; } } } ``` 在上述代码中,我们首先获取了当前用户,然后判断用户是否拥有执行当前 Action 的权限。如果有权限,则继续执行 Action;否则,跳转到错误页面。 接下来,在 struts.xml 配置文件中,我们可以将该拦截器配置为需要进行权限控制的 Action拦截器,如下所示: ```xml <action name="myAction" class="com.example.MyAction"> <interceptor-ref name="authInterceptor"/> <result name="success">/myAction.jsp</result> <result name="error">/error.jsp</result> </action> <interceptor-stack name="authStack"> <interceptor-ref name="authInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> <interceptors> <interceptor name="authInterceptor" class="com.example.AuthInterceptor"/> </interceptors> ``` 在上述代码中,我们首先定义了一个名为 authStack 的拦截器栈,该拦截器栈包含了 authInterceptor 和 defaultStack 两个拦截器。然后,我们将 myAction Action 配置为使用 authStack 拦截器栈。最后,我们定义了一个名为 authInterceptor拦截器,并将其添加到了 interceptors 中。 这样一来,当用户访问 myAction Action 时,就会先执行 authInterceptor 拦截器,进行权限控制,然后再执行 defaultStack 拦截器栈中的其它拦截器。如果 authInterceptor 拦截器返回了 error,则会跳转到 error.jsp 页面;否则,会跳转到 myAction.jsp 页面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值