struts2中拦截器的原理与配置

本文详细介绍了Struts2中拦截器的使用规则、定义和应用,包括如何配置拦截器栈,改变系统默认的拦截器栈,以及拦截器与拦截器栈之间的关系。文章还解释了拦截器在执行action前后的顺序,并提供了实际示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,向大家推荐一个关于拦截器较好的blog:http://developer.51cto.com/art/200906/126894.htm

1.拦截器的使用规则:先定义;再使用;

拦截器的定义:

<interceptors>
			<interceptor name="theInterceptor1" class="com.shengsiyuan.interceptor.TheInterceptor1">
				<param name="test">shengsiyuan</param>
			</interceptor>
			
			<interceptor name="theInterceptor2" class="com.shengsiyuan.interceptor.TheInterceptor2">
			</interceptor>
			
			<interceptor name="theInterceptor3" class="com.shengsiyuan.interceptor.TheInterceptor3">
			</interceptor>
			
			<interceptor name="loginInterceptor" class="com.shengsiyuan.interceptor.LoginInterceptor">
			</interceptor>
</interceptors>

拦截器的使用:

		<action name="action1" class="com.shengsiyuan.struts2.Action1" method="myExecute">
			<result name="success" type="chain">
				<param name="actionName">action2</param>
				<param name="username">${username}</param>
				<param name="password">${password}</param>
				<param name="usernameAndPassword">${usernameAndPassword}</param>
			</result>
			
			<interceptor-ref name="theInterceptor1"></interceptor-ref>
			<interceptor-ref name="theInterceptor2"></interceptor-ref>
			<interceptor-ref name="theInterceptor3">
				<param name="includeMethods">execute, myExecute</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
2.

struts2中有一个系统默认的拦截器栈是 defaultStack,如果你手动引用自己的拦截器,系统默认的拦截器栈将不起作用;这样必需手动引入系统的拦截器栈<interceptor-ref name="defaultStack">

              </interceptor-ref>

如果想改变系统默认的拦截器栈,可以这样配置:

<default-interceptor-ref name="myDefaultInterceptorStack"></default-interceptor-ref>

其中myDefaultInterceptorStack是自己定义的拦截器栈名字;

如果拦截器栈中有多个拦截器,在执行action之前的顺序跟配置拦截器的顺序一致,而在action之后执行的顺序是相反的;

3.拦截器和拦截器栈是一个级别的,也就是说一个拦截器栈中包括许多拦截器一个拦截器栈中还可以包括许多拦截器栈,配置如下方式:

<interceptor-stack name="myDefaultInterceptorStack">
				<interceptor-ref name="loginInterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值