为什么我自定义的拦截器用上includeMethods不起作用呢?请大家帮忙看看,谢谢。
拦截器类:publicclassFirstInterceptorextendsMethodFilterInterceptor{@OverrideprotectedStri...
拦截器类:
public class FirstInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
String className = invocation.getAction().getClass().getName();
long startTime = System.currentTimeMillis();
System.out.println(includeMethods);
System.out.println("拦截器"+name+"在Action" + className +"执行前");
String result = invocation.invoke();
System.out.println("拦截器"+name+"在Action" + className +"执行后总耗时:"+(System.currentTimeMillis()-startTime));
System.out.println(result);
// TODO Auto-generated method stub
return result; }}
配置文件:
<package name="interceptor" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="firstInterceptor" class="interceptor.FirstInterceptor">
<param name="name">the filter interceptor</param>
<param name="includeMethods">method3,execute,method2</param>
</interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="firstInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="exincludeInterceptorAction" class="interceptor.ExIncludeInterceptorAtion" >
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor">
<param name="excludeMethods">execute,method1</param>
<param name="includeMethods">method3,execute,method2</param>
</interceptor-ref>
<result>interceptorResult.jsp</result>
</action>
</package>
action文件:
public String method1() {
ActionContext.getContext().put("msg", "the method1 is invoked");
System.out.println("the method1 is invoked");
return SUCCESS;
}
public String method2() {
ActionContext.getContext().put("msg", "the method2 is invoked");
System.out.println("the method2 is invoked");
return SUCCESS;
}
public String method3() {
ActionContext.getContext().put("msg", "the method3 is invoked");
System.out.println("the method3 is invoked");
return SUCCESS;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
ActionContext.getContext().put("msg", "the execute is invoked");
System.out.println("the execute is invoked");
return SUCCESS;
}
} 展开
public class FirstInterceptor extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
String className = invocation.getAction().getClass().getName();
long startTime = System.currentTimeMillis();
System.out.println(includeMethods);
System.out.println("拦截器"+name+"在Action" + className +"执行前");
String result = invocation.invoke();
System.out.println("拦截器"+name+"在Action" + className +"执行后总耗时:"+(System.currentTimeMillis()-startTime));
System.out.println(result);
// TODO Auto-generated method stub
return result; }}
配置文件:
<package name="interceptor" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="firstInterceptor" class="interceptor.FirstInterceptor">
<param name="name">the filter interceptor</param>
<param name="includeMethods">method3,execute,method2</param>
</interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="firstInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="exincludeInterceptorAction" class="interceptor.ExIncludeInterceptorAtion" >
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor">
<param name="excludeMethods">execute,method1</param>
<param name="includeMethods">method3,execute,method2</param>
</interceptor-ref>
<result>interceptorResult.jsp</result>
</action>
</package>
action文件:
public String method1() {
ActionContext.getContext().put("msg", "the method1 is invoked");
System.out.println("the method1 is invoked");
return SUCCESS;
}
public String method2() {
ActionContext.getContext().put("msg", "the method2 is invoked");
System.out.println("the method2 is invoked");
return SUCCESS;
}
public String method3() {
ActionContext.getContext().put("msg", "the method3 is invoked");
System.out.println("the method3 is invoked");
return SUCCESS;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
ActionContext.getContext().put("msg", "the execute is invoked");
System.out.println("the execute is invoked");
return SUCCESS;
}
} 展开
展开全部
这是因为在配置的时候,只要配置了拦截器,那么默认拦截器就会失效,在那里面重新定义默认拦截器,或者,将你的拦截器和默认的拦截器放在同一个拦截器栈中,然后将你的拦截器栈定义为默认的
<default-interceptor-ref name="defaultStack"/>这是默认的拦截器,在包下,但是不显示,当你引用新 的拦截器的时候就失效了,在你的stack中加入这个defaultStack拦截器就行了,然后将你的拦截器栈定义为默认的
<default-interceptor-ref name="defaultStack"/>这是默认的拦截器,在包下,但是不显示,当你引用新 的拦截器的时候就失效了,在你的stack中加入这个defaultStack拦截器就行了,然后将你的拦截器栈定义为默认的
追问
你好,代码中就是重新在action中定义的默认拦截器啊,而且在includeMethods中添加execute方拦截,自定义的拦截器会进入,却没有拦截method2,method3,因为这两个方法没有输出,去掉execute就不进去为什么不进自定义拦截器呢?我试过,跟上面的情况是一样的啊。指教一下万分感谢
追答
改成这样,把你的execute()
去掉,你添加
只是恢复了默认的情况,你自定义的拦截器并没用配置
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询