Struts2的行动无法执行时,重定向(具有拦截)到其他动作(Struts2 action coul

2019-09-03 20:20发布

我发送一个请求到ActLand ,然后intercept()中,如果在随后没有登录重定向到Login.jsp

struts.xml

<struts>        
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="login" class="com.interceptor.LoginInterceptor"/>
            <interceptor-stack name="loginStack">
                <interceptor-ref name="login"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="loginStack"/>
        <global-results>
            <result name="loginRedirect" type="redirect">/login.jsp</result>
        </global-results>
        <action name="ActLand" class="com.action.ActLand">
            <result name="ok">/index.jsp</result>
        </action>
        <action name="ActLogin" class="com.action.ActLogin">
            <result name="ok">/index.jsp</result>
            <result name="fail">/login.jsp</result>
        </action>                     
    </package>
</struts>

拦截器:

public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();    

        int userId = -1;
        if (session.get("userId") != null) {
            userId = Integer.parseInt(session.get("userId").toString());
        }

        Object action = invocation.getAction();
        if (userId < 0) {                
            return "loginRedirect";
        } else if (!(action instanceof ActLogin)) {
            return "loginRedirect";
        }
        return invocation.invoke();
}

错误:

SEVERE: Could not execute action: /ActLand
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:473)
    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:138)
    at org.apache.struts2.dispatcher.ServletRedirectResult.sendRedirect(ServletRedirectResult.java:217)
    at org.apache.struts2.dispatcher.ServletRedirectResult.doExecute(ServletRedirectResult.java:197)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.components.ActionComponent.executeAction(ActionComponent.java:289)
    at org.apache.struts2.components.ActionComponent.end(ActionComponent.java:172)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.index_jsp._jspx_meth_s_005faction_005f0(index_jsp.java:152)
    at org.apache.jsp.index_jsp._jspx_meth_s_005fif_005f0(index_jsp.java:120)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我很抱歉,我相信我这样做,我奉命到每一件事情,但事情是复杂的...

Answer 1:

添加<interceptor-ref name="defaultStack"/>loginStacklogin拦截。 这将有助于解决重定向结果,而不是仅仅是问题。



Answer 2:

我多么愚蠢是...在拦截只是错误的逻辑。 现在一切都很好!

 public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();

        int userId = -1;
        if (session.get("userId") != null) {
            userId = Integer.parseInt(session.get("userId").toString());
        }


        Object action = invocation.getAction();
        //If Act Login, then let through
          if ((action instanceof ActLogin)) {
            return invocation.invoke();
        }
        //Else, if not login, nor not Actlogin, then redirect
        if (userId < 0) {
            System.out.println("check id session " + userId);
            return "loginRedirect";
        }
        //Else Logined, let through
        return invocation.invoke();
    }

在原来的代码:

 if (userId < 0) {                
    return "loginRedirect";
 } else if (!(action instanceof ActLogin)) {
   return "loginRedirect";
 }

这导致infinitite循环,因为我不是loged中,然后用户id总是<0,则始终重定向,从不通过ActLogin登录让



文章来源: Struts2 action could not execute when redirect (with interceptor) an action to other