Struts2中拦截器的配置

在struts.xml文件中定义拦截器只需要给拦截器类指定一个拦截器名,这就完成了定义。拦截器使用<interceptor>标记来定义,格式如下

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

大部分情况下,如果有一个拦截器这样配置就够了。如果有多个拦截器,则需要写多个<interceptor>,而<interceptor>是写在<interceptors>标记内部,而<interceptors>又是写在struts.xml中的<package>标记中的

<package ...>

<interceptors>

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

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

<interceptor name="拦截器名3" class="拦截器类3"></interceptor>

...

</interceptors>

</package>

如果需要还可以再拦截器<interceptor》标记中间加上参数标记,用来给拦截器定义参数,参数标记是<param>.格式如下

<interceptor name="拦截器名" class="拦截器类">

<param name="参数名">参数值</param>

</interceptor>

其中的参数可以有0个,也可以有多个。这里定义的多个拦截器参数可以再拦截器类中直接获取。获取的方法是在拦截器类中定义名称和参数相同的字段,还要定义字段的seter方法。例如在struts.xml的配置文件中定义如下:

<interceptors>

<interceptor name="helloInterceptor" class="com.inter.HelloInterceptor">

<param name="name">李四</param>

</interceptor>

</interceptors>

拦截器类HelloInterceptor.java如下:

package com.inter;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class HelloInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;

//上面配置拦截器时的参数name

private String name;

public void setName(String name) {

this.name = name;

}

@Override

public String intercept(ActionInvocation arg0) throws Exception {

System.out.println("the interceptor is running...");

String result = arg0.invoke();

System.out.println("获取拦截器中的参数信息是"+name);

System.out.println("the interceptor is ending...");

return result;

}

}

输出的信息如下:

the interceptor is running...

正在执行action...

获取拦截器中的参数信息是李四

the interceptor is ending...

可以将多个定义好的拦截器组织成一个拦截器栈,拦截器栈使用<interceptor-stack>标记定义。配置拦截器栈的格式如下:

<interceptor-stack name="拦截器栈名">

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

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

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

...

</interceptor-stack>

下面是Struts2中核心配置文件struts-default.xml中的部分代码,从中我们可以看出拦截器栈的配置方式

<interceptors>

<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>

<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>

<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>

<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>

<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>

<interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />

<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />

<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />

<interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>

<!--省略部分代码-->

<!-- Basic stack -->

<interceptor-stack name="basicStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="multiselect"/>

<interceptor-ref name="actionMappingParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

</interceptor-stack>

<!-- Sample validation and workflow stack -->

<interceptor-stack name="validationWorkflowStack">

<interceptor-ref name="basicStack"/>

<interceptor-ref name="validation"/>

<interceptor-ref name="workflow"/>

</interceptor-stack>

<!--省略部分代码-->

<!-- An example of the paramsPrepareParams trick. This stack

is exactly the same as the defaultStack, except that it

includes one extra interceptor before the prepare interceptor:

the params interceptor.

This is useful for when you wish to apply parameters directly

to an object that you wish to load externally (such as a DAO

or database or service layer), but can‘t load that object

until at least the ID parameter has been loaded. By loading

the parameters twice, you can retrieve the object in the

prepare() method, allowing the second params interceptor to

apply the values on the object. -->

<interceptor-stack name="paramsPrepareParamsStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="multiselect"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="actionMappingParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>

<!-- A complete stack with all the common interceptors in place.

Generally, this stack should be the one you use, though it

may do more than you need. Also, the ordering can be

switched around (ex: if you wish to have your servlet-related

objects applied before prepare() is called, you‘d need to move

servletConfig interceptor up.

This stack also excludes from the normal validation and workflow

the method names input, back, and cancel. These typically are

associated with requests that should not be validated.

-->

<interceptor-stack name="defaultStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="debugging"/>

<interceptor-ref name="scopedModelDriven"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="multiselect"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="actionMappingParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>

<!--省略部分代码-->

</interceptors>

其中,多个已经定义的拦截器栈构成拦截器栈。拦截器栈不仅可以由基本拦截器构成,还可以由其他的拦截器栈构成,例如:

<interceptor-stack name="拦截器栈名1">

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

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

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

...

</interceptor-stack>

<interceptor-stack name="拦截器栈名2">

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

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

<interceptor-ref name="拦截器栈3"/>

...

</interceptor-stack>

上面两个拦截器栈,其中第一个拦截器栈是由三个拦截器构成,而第二个拦截器栈是由三个拦截器栈构成

使用拦截器

拦截器的使用相对比较简单,只需要在struts.xml文件的action中添加<interceptor-ref>标记,用于指定具体使用哪

一个定义好的拦截器即可。

<action name="helloaction" class="com.action.HelloAction">

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

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

<interceptor-ref name="defaultStack"></interceptor-ref>

<interceptor-ref name="helloInterceptor"></interceptor-ref>

</action>

其中<interceptor-ref name="defaultStack"></interceptor-ref>是系统的默认拦截器,当用户自定义了拦截器后,系统默认的拦截器会自动失效。为了能够使用系统默认的拦截器,应该把它加上。

时间: 2024-08-11 05:33:20

Struts2中拦截器的配置的相关文章

struts2中拦截器的简介与配置使用

拦截器是struts2框架的核心,struts2很多的功能都是构建在拦截器基础之上的,它是动态拦截Action调用的对象,提供了一种机制,使得开发者能够在一个Action前后执行需要的代码,可以在一个Action执行前组织他的执行,也能在Action执行后做一些相应的工作.同时他也提供了一种可以提取Action中可重用部分的方式. 拦截器 struts2拦截器是在访问某个Action或它的某个方法 .字段之前或之后实施拦截,struts2拦截器是可插拔的,是AOP的一种实现(AOP是OOP(Ob

Struts2中拦截器的简单实现

Struts2的拦截器和Servlet过滤器类似.在执行Action的execute方法之前,Struts2会首先执行struts.xml中引用 的拦截器,在执行完所有引用的拦截器的intercept方法后,会执行Action的execute方法.在Struts2的拦截器体系中, Struts2的内建拦截器完成了该框架的大部分操作,所以实际的开发过程中通常都是使用系统的拦截器而已.当然我们也可以开发自己的拦截器,类似于过滤器的开发. 原理: Struts2拦截器的实现原理相对简单,当请求Stru

struts2中拦截器(interceptor)小结

什么是拦截器? java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重用部分的方式.在AOP(Aspect-Oriented Programming)中拦截器用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.Struts2内置了很多拦截器,每个拦截器完成相对独立的功能,多个拦截器的组合体成为拦截器栈.最为重要的拦截器栈是

struts2中拦截器与过滤器之间的区别

首先是一张经典的struts2原理图 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) FilterDispatecher会去查找相应的ActionMapper,如果找到了相应的ActionMapper它将会将控制权限交给ActionProxy d) ActionProxy将会通过ConfigurationManager来查找配置struts.xml        i. 下一步将会

Struts2中拦截器的使用

拦截器是Struts2框架的核心组成部分,框架的很多功能都是构建在拦截器基础之上.Struts2利用内建的拦截器完成了框架内的大部分操作,拦截器是AOP(面向方面编程)在Struts2框架中的具体实现. 下面是配置自定义拦截器的流程. 假设需要一个功能,在调用每个Action之前都能在控制台打印出Helloworld,要求使用Struts2拦截器来实现. 1.建立一个Action类MyAction.java和一个配置文件struts.xml,如下图. 2.编写MyAction.java实现一个年

说说struts2中拦截器的请求流程一(模拟大致流程)

本文可作为北京尚学堂struts2课程的学习笔记. 首先 什么是拦截器?拦截器能干什么? 拦截器,顾名思义就是拦截对象然后做操作的东西,至于是拦截谁?那自然是拦截action了.能做什么操作呢?你想让action在运行之前干什么就能干什么,而且action本身并"不知道"自己被拦截了. 文章主要分析了拦截器部分的流程,对于环境的获取与初始化并没有涉及,对这部分感兴趣的朋友可以参考 http://www.cnblogs.com/liuling/p/2013-8-10-01.html 在真

Struts2中拦截器

创建拦截器 package com.zhangpn.Intercepor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class PermissionIntercepor implements Interceptor{ @Ov

struts2中拦截器和过滤器的比较

拦截器和过滤器的区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用 4.拦截器可以访问action上下文.值栈里的对象,而过滤器不能 5.在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次 拦截器 :是在面向切面编程(AOP)的就是在你的service或者一个方法前调用一个方法,或者在方法后调用

转载 - Struts2 拦截器详细配置过程

出处:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法(init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一次; Destroy()方法:当拦截器销毁时执行的方法; Interceptor()方