使用自定义的拦截器
继承其中的两个类较为方便:
MethodFilterInterceptor
该类有两个可以设置是拦截具体方法,这是AbstractInterceptor所不具备的,使用只需要在struts.xml中静态注入参数即可
setIncludeMethods(String includeMethods):设置需要拦截的方法,多个方法用逗号分隔
setExcludeMethods(String excludeMethods):设置不需要拦截的方法,多个方法用逗号分隔
例如伪代码:
<action name="名称" class="动作类全名">
<param name="拦截器类名.excludeMethods">方法名</param>
</action>#即该拦截器不拦截该方法
@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
//放行
String s = invocation.invoke();
// 返回的逻辑视图
return s;
}
AbstractInterceptor
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//放行
String s = invocation.invoke();
// 返回的逻辑视图
return s;
}
例如伪代码:
如果该类需要一些动作方法跳过拦截,则需要在struts.xml中的需要拦截的动作方法
<action name="需要拦截的方法" class="动作方法的类全名">
<intercept-ref name="defaultStack"></intercept-ref>
<intercept-ref name="自定义拦截器配置的名称"></intercept-ref>
</action>