struts2 拦截器的invoke方法

新手上路,请多包涵

刚刚学习拦截器,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>
阅读 4.2k
2 个回答
新手上路,请多包涵

经过验证,跳转页面与one.java返回值有关系,为什么会出现这种情况呢

我看到书上说调用了invoke()方法,流程就会离开这个拦截器,往下一个拦截器或者action执行了。然后所以有invoke()应该就不会return error了,按照你写的action会返回success。没有invoke()应该会返回error。所以就给出的代码来看,对于不加invoke()还能跳转到index我也很迷茫

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进