刚刚学习拦截器,invoke方法有一点没有弄明白。
在一个简单的测试中,在intercept中加上invoke方法和不加出现结果与预想的不同。
定义拦截器返回“error”,应跳转到error.jsp,但加上invoke方法后跳转至success.jsp页面(应该返回为“success”时跳转到主页)
初学者,百度但没有弄明白,望各位老师指导,耽误大家时间,请海涵。
Interceptor1.java
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class Interceptor1 implements Interceptor {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("this is interceptor1");
arg0.invoke();//为什么在使用此拦截器时,return error,不加invoke返回为index.jsp(应该返回success时才为index.jsp)
return "error";
}
}
struts.xml配置
<interceptors>
<interceptor name="interc1" class="first1.Interceptor1"></interceptor>
</interceptors>
<!--全局跳转 -->
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="oneAction" class="first1.one">
<interceptor-ref name="interc1"></interceptor-ref>
<result name="success">/index.jsp</result>
</action>
one.java
import com.opensymphony.xwork2.Action;
public class one implements Action {
@Override
public String execute() throws Exception {
System.out.println("this is the one action");
return "success";
}
}
error.jsp
<html>
<head></head>
<body>
<h1>error!</h1><br>
</body>
</html>
经过验证,跳转页面与one.java返回值有关系,为什么会出现这种情况呢