问题描述
struts2使用拦截器(自定义拦截器)时出现以下问题:当不使用拦截器时页面正常跳转,当使用拦截器后页面总是跳转到<result name="error">/error.jsp</result>页面,不管输入的是正确还是错误!请各位解答是怎么回事,谢谢了!附相关代码:struts.xml:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="struts2Demo" extends="struts-default"><interceptors><interceptor name="myinterceptor" class="interceptor.MyInterceptor"></interceptor></interceptors><action name="myaction" method="execute" class="action.MyTestAction"><result name="success">/success.jsp</result><result name="error">/error.jsp</result><interceptor-ref name="myinterceptor"></interceptor-ref></action></package></struts> MyInterceptor.java:package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class MyInterceptor extends AbstractInterceptor {@Overridepublic String intercept(ActionInvocation invocation) throws Exception {long start = System.currentTimeMillis();String res = invocation.invoke();long end = System.currentTimeMillis();System.out.println("执行"+ invocation.getClass().getName()+"耗时"+(end-start)+"ms");return res;}}
解决方案
<action name="myaction" method="execute" class="action.MyTestAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myinterceptor"></interceptor-ref> </action> 这样就对了
解决方案二:
<interceptor-ref name="defaultStack"/><interceptor-ref name="myinterceptor"></interceptor-ref> 你应该在自己的拦截器前面先引用默认的拦截器
解决方案三:
前面加上 <interceptor-ref name="defaultStack"/>因为你自定义了 默认的就失效了