struts2 Interceptors 拦截器

本文详细介绍了拦截器的工作原理及如何在Struts框架中进行配置。包括了如何使用拦截器来执行代码,在Action调用前后进行操作,以及如何通过配置文件指定特定的拦截器和拦截器栈。此外还涉及了方法过滤拦截器的使用方法。

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

拦截器原理

Understanding Interceptors

Interceptors can execute code before and after an Action is invoked. Most of the framework's core functionality is implemented as Interceptors. Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.

Interceptors can be configured on a per-action basis. Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework. Interceptors "set the stage" for the Action classes, doing much of the "heavy lifting" before the Action executes.




拦截器配置struts.xml

	<package name="default" extends="struts-default">
		<interceptors>
			<interceptor name="timer" class=".." />
			<interceptor name="logger" class=".." />
		</interceptors>
		<action name="login" class="tutorial.Login">
			<interceptor-ref name="timer" />
			<interceptor-ref name="logger" />
			<result name="input">login.jsp</result>
			<result name="success" type="redirectAction">/secure/home</result>
		</action>
	</package>

拦截器栈

	<package name="default" extends="struts-default">
		<interceptors>
			<interceptor name="timer" class=".." />
			<interceptor name="logger" class=".." />
			<interceptor-stack name="myStack">
				<interceptor-ref name="timer" />
				<interceptor-ref name="logger" />
			</interceptor-stack>
		</interceptors>
		<action name="login" class="tutuorial.Login">
			<interceptor-ref name="myStack" />
			<result name="input">login.jsp</result>
			<result name="success" type="redirectAction">/secure/home</result>
		</action>
	</package>

方法过滤

MethodFilterInterceptor is an abstract Interceptor used as a base class for interceptors that will filter execution based on method names according to specified included/excluded method lists.

Settable parameters are as follows:

  • excludeMethods - method names to be excluded from interceptor processing
  • includeMethods - method names to be included in interceptor processing

NOTE: If method name are available in both includeMethods and excludeMethods, it will be considered as an included method: includeMethods takes precedence over excludeMethods.

Interceptors that extends this capability include:

  • TokenInterceptor
  • TokenSessionStoreInterceptor
  • DefaultWorkflowInterceptor
  • ValidationInterceptor


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值