`
zjcheng
  • 浏览: 91978 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2中的拦截器(转)

阅读更多
一.理解拦截器

   1. 拦截器是在防问某个方法,字段之前或之后实施拦截,并且拦截器是可插拔的,拦截器是AOP的一种实现.

   2. 拦截器栈(Interceptor Stack)。拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,       拦截器链中的拦截器就会按其之前定义的顺序被调用。

二.实现原理
    Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的    拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器

三.拦截器的配置
  1. 普通的拦截器

Xml代码
1.<package name="default" extends="struts-default"><BR>  
2.  
3.  
4.   <interceptors><BR>  
5.  
6.  
7.       <interceptor name="拦截器名1" class="拦截器实现类"/><BR>  
8.  
9.  
10.       <interceptor name="拦截器名2" class="拦截器实现类"/><BR>  
11.  
12.  
13.   </interceptors><BR>  
14.  
15.  
16.<action name="login" class="com.Logon"><BR>  
17.  
18.  
19.  <interceptor-ref name="defaultStack"/><BR>  
20.  
21.  
22.        <interceptor-ref name="拦截器名1"/><BR>  
23.  
24.  
25.        <interceptor-ref name="拦截器名2"/><BR>  
26.  
27.  
28.         <result name="input">logon.jsp</result><BR>  
29.  
30.  
31.         <result name="success">/index.jsp</result><BR>  
32.  
33.  
34.   </action><BR>  
35.  
36.  
37.</package>  
<package name="default" extends="struts-default">


   <interceptors>


       <interceptor name="拦截器名1" class="拦截器实现类"/>


       <interceptor name="拦截器名2" class="拦截器实现类"/>


   </interceptors>


<action name="login" class="com.Logon">


  <interceptor-ref name="defaultStack"/>


        <interceptor-ref name="拦截器名1"/>


        <interceptor-ref name="拦截器名2"/>


         <result name="input">logon.jsp</result>


         <result name="success">/index.jsp</result>


   </action>


</package> 

2. 拦截器栈

Xml代码
1.<package name="default" extends="struts-default"><BR>  
2.  
3.  
4.   <interceptors><BR>  
5.  
6.  
7.        <interceptor name="拦截器名1" class="拦截器实现类"/><BR>  
8.  
9.  
10.        <interceptor name="拦截器名2" class="拦截器实现类"/><BR>  
11.  
12.  
13.        <interceptor-stack name="myStack"><BR>  
14.  
15.  
16.           <interceptor-ref name="拦截器名1"/><BR>  
17.  
18.  
19.           <interceptor-ref name="拦截器名2"/><BR>  
20.  
21.  
22.        </interceptor-stack><BR>  
23.  
24.  
25.    </interceptors><BR>  
26.  
27.  
28.    <action name="login" class="com.Logon"><BR>  
29.  
30.  
31.     <interceptor-ref name="defaultStack"/><BR>  
32.  
33.  
34.         <interceptor-ref name="myStack"/><BR>  
35.  
36.  
37.         <result name="input">login.jsp</result><BR>  
38.  
39.  
40.         <resultnameresultname="success" >/index.jsp</result><BR>  
41.  
42.  
43.</action><BR>  
44.  
45.  
46.</package><BR>  

<package name="default" extends="struts-default">


   <interceptors>


        <interceptor name="拦截器名1" class="拦截器实现类"/>


        <interceptor name="拦截器名2" class="拦截器实现类"/>


        <interceptor-stack name="myStack">


           <interceptor-ref name="拦截器名1"/>


           <interceptor-ref name="拦截器名2"/>


        </interceptor-stack>


    </interceptors>


    <action name="login" class="com.Logon">


     <interceptor-ref name="defaultStack"/>


         <interceptor-ref name="myStack"/>


         <result name="input">login.jsp</result>


         <resultname="success" >/index.jsp</result>


</action>


</package>


需要注意的是,如果为Action指定了一个拦截器,则系统默认的拦截器栈将会失去作用。为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器



四.自定义拦截器
   1.直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。
   2.或者继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor
   3.通过<interceptor>元素来定义拦截器
   4.通过<interceptor-ref>元素来使用拦截器

五.使用拦截器实现权限控制
  1.功能
    使用自定义拦截器来完成用户权限的控制,当执行操作时,判断用户是否己经登陆,如果没有登陆跳转到登陆页面

  2.拦截器类

Java代码
1.package com.interceptor;   
2.<BR>   
3.<BR>import com.opensymphony.xwork2.Action;   
4.<BR>import com.opensymphony.xwork2.ActionContext;   
5.<BR>import com.opensymphony.xwork2.ActionInvocation;   
6.<BR>import com.opensymphony.xwork2.interceptor.AbstractInterceptor;   
7.<BR>   
8.<BR>public class AuthorizationInterceptor extends AbstractInterceptor {   
9.<BR>   
10.<BR>    private static final long serialVersionUID = 1L;   
11.<BR>   
12.<BR>    @Override  
13.<BR>    public String intercept(ActionInvocation actionInvocation) throws Exception {   
14.<BR>           
15.<BR>        ActionContext actionContext = actionInvocation.getInvocationContext();   
16.<BR>           
17.<BR>        Object user = actionContext.get("user");   
18.<BR>           
19.<BR>        if(user != null){   
20.<BR>            return actionInvocation.invoke();   
21.<BR>        } else{   
22.<BR>            actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆");   
23.<BR>            return Action.LOGIN;   
24.<BR>        }   
25.<BR>    }   
26.<BR>   
27.<BR>}   
28.<BR>  
package com.interceptor;

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

public class AuthorizationInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		
		ActionContext actionContext = actionInvocation.getInvocationContext();
		
		Object user = actionContext.get("user");
		
		if(user != null){
			return actionInvocation.invoke();
		} else{
			actionInvocation.getInvocationContext().put("nav_title", "你还没有登陆,请先登陆");
			return Action.LOGIN;
		}
	}

}


2.配置

Xml代码
 
1.<?xml version="1.0" encoding="UTF-8" ?><BR>  
2.  
3.  
4.<!DOCTYPE struts PUBLIC<BR>  
5.  
6.  
7.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<BR>  
8.  
9.  
10.    "http://struts.apache.org/dtds/struts-2.0.dtd"><BR>  
11.  
12.<struts>  <BR>  
13.   <package name="user" extends="struts-default">  
14.  
15.        <interceptors>  
16.            <!-- 定义拦截器  -->  
17.            <interceptor name="authority" class="com.interceptor.AuthorizationInterceptor"></interceptor>  
18.        </interceptors><BR>  
19.  
20.        <action name="user" class="com.UserAction"><BR>  
21.            <!-- 使用拦截器 -->  
22.            <interceptor-ref name="authority"/>  
23.            <interceptor-ref name="defaultStack"/>  
24.            <result name="succee">/logon/welcome.jsp</result><BR>  
25.            <result name="login">/logon/logon.jsp</result>  
26.        </action>  
27.    </package>  
28.</struts><BR>  
<?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="user" extends="struts-default">

    	<interceptors>
    		<!-- 定义拦截器  -->
    		<interceptor name="authority" class="com.interceptor.AuthorizationInterceptor"></interceptor>
    	</interceptors>

        <action name="user" class="com.UserAction">
        	<!-- 使用拦截器 -->
        	<interceptor-ref name="authority"/>
        	<interceptor-ref name="defaultStack"/>
            <result name="succee">/logon/welcome.jsp</result>
            <result name="login">/logon/logon.jsp</result>
        </action>
    </package>
</struts>

访问http://localhost:8080/struts2-interceptor/user.action时,会判断用户是否登陆


六.方法拦截器
  1.Struts2提供MethodFilterInterceptor类,该类是AbstractInerceptor的子类,可以实现对Action方法的拦截.
  2. MethodFilterInterceptor中有两个方法
   setExcludeMethods:排除需要过滤的方法
     setIncludeMethods:设置需要过滤的方法
     如果一个方法同时在excludeMethods和includeMethods中出现,则会被拦截

  3.实现

拦截器

Java代码
1.
package com.interceptor;   
2.<BR>   
3.<BR>import com.opensymphony.xwork2.ActionInvocation;   
4.<BR>import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;   
5.<BR>   
6.<BR>public class LogInterceptor extends MethodFilterInterceptor {   
7.<BR>   
8.<BR>    private static final long serialVersionUID = 1L;   
9.<BR>   
10.<BR>    private String name;   
11.<BR>       
12.<BR>    @Override  
13.<BR>    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {   
14.<BR>       
15.<BR>        System.out.println("拦截器名称:" + name);   
16.<BR>        System.out.println("action:" + actionInvocation.getAction());   
17.<BR>           
18.<BR>        return actionInvocation.invoke();   
19.<BR>    }   
20.<BR>   
21.<BR>    public String getName() {   
22.<BR>        return name;   
23.<BR>    }   
24.<BR>   
25.<BR>    public void setName(String name) {   
26.<BR>        this.name = name;   
27.<BR>    }   
28.<BR>}   
29.<BR>  
package com.interceptor;

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

public class LogInterceptor extends MethodFilterInterceptor {

	private static final long serialVersionUID = 1L;

	private String name;
	
	@Override
	protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
	
		System.out.println("拦截器名称:" + name);
		System.out.println("action:" + actionInvocation.getAction());
		
		return actionInvocation.invoke();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
 

 action

Java代码 
1.package com;<BR><BR>   
2.public class ManageAction {<BR><BR>   
3.    public String execute(){   
4.        System.out.println("execute....");<BR>   
5.        return "succee";   
6.    }<BR>   
7.  
8.    public String search(){<BR>   
9.        System.out.println("search....");   
10.        return "succee";   
11.    }   
12.    public String add(){   
13.        System.out.println("add....");   
14.        return "succee";   
15.    }   
16.}<BR>  
package com;
public class ManageAction {
	public String execute(){
		System.out.println("execute....");
		return "succee";
	}

	public String search(){
		System.out.println("search....");
		return "succee";
	}
	public String add(){
		System.out.println("add....");
		return "succee";
	}
}

struts.xml配置

Xml代码
1.<action name="manage" class="com.ManageAction"><BR> 
2.            <interceptor-ref name="log"><BR> 
3.                <param name="name">日志拦截</param><BR> 
4.                <!-- 设置需要拦截的方法,指定多个方法以逗号隔开 --> 
5. 
6.                <param name="includeMethods">execute,add</param><BR> 
7.                <!-- 设置不需要拦截的方法,execute在includeMethods中同时存在,execute会被拦截 --><BR> 
8.                <param name="excludeMethods">search,execute</param> 
9.            </interceptor-ref><BR> 
10.            <result name="succee">/welcome.jsp</result> 
11.         </action> 
<action name="manage" class="com.ManageAction">
         <interceptor-ref name="log">
         <param name="name">日志拦截</param>
         <!-- 设置需要拦截的方法,指定多个方法以逗号隔开 -->

         <param name="includeMethods">execute,add</param>
         <!-- 设置不需要拦截的方法,execute在includeMethods中同时存在,execute会被拦截 -->
         <param name="excludeMethods">search,execute</param>
         </interceptor-ref>
         <result name="succee">/welcome.jsp</result>
         </action>



打开浏览器访问 http://localhost:8080/struts2-interceptor/manage.action


会报执行execute方法,会执行拦截器
拦截器名称:日志拦截
action:com.ManageAction@1a0ae6d
execute....

当访问 http://localhost:8080/struts2-interceptor/manage!search.action
执行search方法,不会执行拦截器
分享到:
评论

相关推荐

    Struts2拦截器(Interceptor)

    Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)

    Struts2拦截器及其用法详细说明

    在Struts2中,拦截器(Interceptors)扮演着核心角色,增强了框架的功能和灵活性。这篇文章将深入探讨Struts2拦截器的概念、工作原理以及如何在实际应用中使用它们。 **一、什么是Struts2拦截器** 拦截器是基于AOP...

    struts2 拦截器

    2. **拦截器链**:在Struts2中,多个拦截器可以形成一个拦截器链,每个拦截器按照定义的顺序依次执行。如果所有拦截器都允许Action执行,那么Action的结果将被传递到下一个拦截器,直到整个链执行完毕。 ### 二、...

    struts2中拦截器的使用

    在Struts2中,拦截器的使用主要基于两个方面:配置文件中的声明式使用和注解的编程式使用。首先,我们来看看配置文件中的声明式使用。在struts.xml或类似的配置文件中,你可以通过`&lt;interceptor&gt;`元素定义拦截器,并...

    Struts2编码拦截器

    解决Struts2中的中文乱码。该代码是用作Struts2的拦截器中

    Struts2拦截器源码

    首先,理解拦截器的定义:拦截器是AOP(面向切面编程)的一个概念,在Struts2中,拦截器是基于Java的动态代理机制实现的。它们是一系列实现了`Interceptor`接口的类,可以在Action执行前后插入额外的行为。这些行为...

    struts2实现拦截器、

    在Struts2中,拦截器(Interceptor)是一个至关重要的概念,它允许开发者在动作执行前后插入自定义逻辑,比如权限检查、日志记录、性能监控等。本文将深入探讨如何使用Struts2实现拦截器,以及如何配置拦截器来实现...

    Struts2学习案例(拦截器)

    在Struts2中,拦截器是基于Java的动态AOP(面向切面编程)实现的,它可以在Action调用前后插入额外的逻辑,比如日志记录、权限验证、事务管理等。拦截器通过配置文件或者注解与Action关联,形成一个拦截器栈,每个...

    struts2拦截器

    在Struts2中,拦截器(Interceptor)是核心功能之一,用于增强应用的功能和处理业务逻辑。拦截器是基于AOP(面向切面编程)的概念,可以在动作执行前后插入额外的操作,比如日志记录、权限检查、数据验证等。 标题...

    struts2 拦截器实例

    在Struts2中,拦截器(Interceptor)扮演着核心角色,它们允许开发者在Action执行前后插入自定义的逻辑,如日志、权限检查、事务管理等。现在我们将深入探讨Struts2的拦截器机制及其实例应用。 ### 一、Struts2拦截...

    详解Struts2拦截器

    拦截器不仅在Struts2中扮演着重要角色,更是整个框架灵活性与扩展性的基石。本文将深入探讨Struts2拦截器的基本概念、工作原理以及其实现机制,并结合实际应用场景来展示如何利用拦截器提高代码的复用性与可维护性。...

    使用struts2拦截器对登陆权限验证

    在Struts2中,拦截器(Interceptor)扮演着至关重要的角色,它允许开发者在动作执行前后插入自定义逻辑,如日志记录、权限验证等。在本案例中,我们将深入探讨如何使用Struts2拦截器实现登录权限验证,同时结合...

    Struts2拦截器实现权限控制demo

    在Struts2中,拦截器是实现业务逻辑控制和增强功能的重要机制,它们扮演着类似于AOP(面向切面编程)的角色,允许在动作执行前后插入自定义逻辑。在这个“Struts2拦截器实现权限控制demo”中,我们将深入探讨如何...

    struts2拦截器应用小例子

    在Struts2中,拦截器(Interceptor)扮演着核心角色,它们允许开发者在动作执行前后插入自定义的逻辑,如日志、权限检查、数据验证等。本示例将探讨如何在Struts2中使用拦截器。 首先,我们需要理解Struts2拦截器的...

    struts2常用拦截器

    struts2常用拦截器,struts2经常用到的拦截器,熟悉熟悉

    Struts2 拦截器

    在Struts2中,拦截器就像过滤器一样工作,通过链式调用在动作执行前后进行预处理和后处理。 首先,我们来理解一下拦截器的基本概念。拦截器是在Action调用之前和之后执行的一段代码,可以用来做日志记录、权限检查...

    struts2注解与拦截器demo

    该例子为struts2注解与拦截器demo,利用myEclipse8.5开发,导入刚才后,自动加载所需struts2的jar包,可以直接运行,是初学struts2注解、拦截器很好的例子,保证10分钟学会2种技术,愿意分享给大家。

    难经3:Struts2,拦截器拦不住Result?

    拦截器是Struts2中的一个关键概念,它允许开发者在Action执行前后插入自定义的处理逻辑。例如,可以使用拦截器实现登录验证、日志记录、性能监控等功能。拦截器通过链式结构串联起来,形成一个执行栈,每个Action...

    简单理解Struts2中拦截器与过滤器的区别及执行顺序

    Struts2 中拦截器与过滤器的区别及执行顺序 Struts2 中的拦截器(Interceptor)和过滤器(Filter)是两个不同的概念,虽然它们都可以影响请求的处理过程,但它们的作用域、执行顺序和实现机制都有所不同。 拦截器...

    struts2类型转换 拦截器 校验的例子

    在Struts2中,用户通过表单提交的数据通常是字符串,而服务器端处理时往往需要将其转化为其他类型,如整型、浮点型或日期等。Struts2内置了一套类型转换机制,能够自动将字符串数据转化为预设的类型。如果默认的转换...

Global site tag (gtag.js) - Google Analytics