struts 2 拦截器(Interceptor)

Struts 2的大部分功能都是通过拦截器实现的,当StrutsPrepareAndExecuteFilter()拦截到用户请求时,大量拦截器会对请求进行处理,最后才发送给Action。


一般在Struts.xml文件中的package标签后extend struts-default就可以使用全部struts已经提供的拦截器


struts 2 的拦截器是可插拔的,当需要扩展struts 2的功能时,只需要将相应的拦截器配置在struts容器中即可


细读下面注释:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<constant name="struts.custom.i18n.resources"
		value="messageResource"/>
	<constant name="struts.i18n.encoding" value="GBK"/>


	<!--struts-default中包含大量拦截器  -->
	<package name="lee" extends="struts-default">


		<!-- 配置系统的拦截器 -->
		<interceptors>
			<!-- 定义系统的登录控制的拦截器 -->
			<interceptor name="authority" 
				class="qdu.ssmm.query.action.interceptor.AuthorityInterceptor"/>
				
			<!-- 定义系统auctionStack拦截器栈,引入了一个系统的拦截器(包含很多拦截器)
				还引入了一个自定义拦截器 -->
			<interceptor-stack name="auctionStack">
				<!-- 引用系统的默认拦截器栈 -->
				<interceptor-ref name="defaultStack"/>
				<!-- 引用登录控制拦截器 -->
				<interceptor-ref name="authority"/>
			</interceptor-stack>
		</interceptors>
		
		<!-- 将自定义的拦截器栈定义成默认拦截器,对包内的所有action有效(除非该action单独配置了拦截器)
			避免在每个action中重复定义拦截器 -->
		<default-interceptor-ref name="auctionStack"/>
		
		<!-- 考虑到会有多个Action返回相同的处理结果(login exception),所以配置成的
		<global-results>,在包范围内都有效 -->
		<global-results>
			<result name="login">/WEB-INF/content/login.jsp</result>
			<result name="exception">/WEB-INF/content/error.jsp</result>
		</global-results>


		<!--配置发生异常时对应的视图信息 global-exception-mappings是包范围内的-->
		<global-exception-mappings>
			<!-- 指Action抛出AuctionException异常时,转入名为exception的结果。 -->
			<exception-mapping exception="qdu.ssmm.query.exception.AuctionException" result="exception"/>
			<!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
			<exception-mapping exception="java.lang.Exception" result="exception"/>
		</global-exception-mappings>




		<!-- 若action中没有指定具体的class,默认引用<default-class-ref>元素中所指的类
			系统默认的class为ActionSupport -->
		<action name="index">
			<result name="success">/WEB-INF/content/main.jsp</result>
		</action>


		<action name="mgrKind" class="qdu.ssmm.query.action.MgrKindAction">
			<result name="success">/WEB-INF/content/mgrKind.jsp</result>	
		</action>


		<action name="viewKind" class="qdu.ssmm.query.action.ViewKindAction">
			<result name="success">/WEB-INF/content/viewKind.jsp</result>	
		</action>
		<action name="findKind" class="qdu.ssmm.query.action.FindKindAction">
			<result name="success">/WEB-INF/content/search.jsp</result>	
		</action>


		<!-- 配置处理用户登录的Action -->
		<action name="processLogin" class="qdu.ssmm.query.action.LoginAction">
			<!-- 配置处理用户登录后的Result -->
			<result name="success">/WEB-INF/content/main.jsp</result>
			<result name="failure">/WEB-INF/content/login.jsp</result>
			<!-- 显式引用系统默认拦截器栈,
				这时在<default-interceptor-ref name="auctionStack"/> 进行的默认设置被覆盖掉。
				此处单独为该action设置拦截器是必须的,若仍然用自定义的拦截器栈作为默认拦截器会导致永远登录不上,
				因为在调用登录的action之前也会被拦截器拦截发现未登录而转到login.jsp(哈哈)
				-->
			<interceptor-ref name="defaultStack"/>
		</action>


	
		<action name="proAddKind" class="qdu.ssmm.query.action.AddKindAction">
			<result name="input" type="chain">mgrKind</result>	
			<result name="success" type="chain">mgrKind</result>
		</action>


	</package> 


</struts>


拦截器定义的顺序先后决定起作用的先后。

注意该图中在execute()方法之前拦截器1先起作用,在execute()方法之后拦截器2先起作用




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值