一、自定义**
默认的**能实现的功能是有限的,Struts2 支持自定义**。
二、**类
1.实现 Interceptor 接口
2.继承 AbstractInterceptor 抽象类,需要实现 public String intercept(ActionInvocation actionInvocation) 方法,其中通过 actionInvocation.invoke() 继续调用后续** 和 Action 方法。
Struts2 会自动跳转到自定义**的 interceptor 方法返回值对应的 result,如果直接返回一个 String,那么会将控制器交给目标 action 对应的 result。
3.注册自定义**与使用
(1)Action 级
<package name="default" namespace="/" extends="struts-default"> <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/> <action name="interceptor" class="com.nucsoft.struts.token.InterceptorAction"> <interceptor-ref name="myInterceptor"/> <interceptor-ref name="defaultStack"/> <result>/success.jsp</result> <result name="input">/error.jsp</result> </action>
</package>
(2)package 级
<package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="myInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myInterceptorStack"/> <action name="myInterceptor" class="com.nucsoft.struts.token.InterceptorAction" method="myInterceptor"> <result>/success.jsp</result> <result name="input">/error.jsp</result> </action></package>
原标题:struts2基础——自定义拦截器
关键词:Struts