Struts2 action could not execute when redirect (with interceptor) an action to other
Asked Answered
P

2

2

I send a request to the ActLand, then intercept(), if not logged in then redirect to Login.jsp.

The 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>

The interceptor:

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();
}

The Error:

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)

I'm sorry, I believe I did every thing that I had been instructed to, but things are complicated...

Photoperiod answered 16/5, 2013 at 19:34 Comment(0)
S
1

Add <interceptor-ref name="defaultStack"/> to the loginStack after login interceptor. It will help to solve the problem with redirect result and not only.

Somniloquy answered 16/5, 2013 at 20:14 Comment(13)
<interceptor-stack name="loginStack"> <interceptor-ref name="login"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> Still not working? don't know what happen...Photoperiod
index.jsp is just a regular jsp, which has this tag <s:action name="ActLand" executeResult="true"/> in the first line.Photoperiod
What result return the action named ActLand?Somniloquy
in ActLand execute() method, I only return "ok". It load a List of Lands from a LandDAO, like: list = new LandDAO().loadAll(); return "ok";Photoperiod
It calls the action in the cycle.Somniloquy
how can that happen? didn't the interceptor means to intercept an action to act? I thought like... before ActLand can do anything, the interceptor will does it works first, then stop actLand to do anything, redirect blah blah? Then what should I do properly?Photoperiod
Remove s:action tag from JSP and replace it with servlet redirect or http meta refresh, remove results with location "/index.jsp".Somniloquy
Hey~!! This work!! <% response.sendRedirect("ActLand"); %> And comment the result "ok" in struts.xml. But can the ActLand still working after that?Photoperiod
No, it doesn't have result then you need to give it forward (default) result to the other jsp or type="redirectAction" result to action.Somniloquy
Argg! How stupid I am!! I just solved it... the logic in interceptor is wrong... Really sorry... how dump :D Thank you very much... You helped a lot!Photoperiod
I have posted an interceptor for login in some of my answers, it might help you with other things too.Somniloquy
Thanks very much, I will find it in your answers... I'm working on a project using struts2... I will ask you some later, I think :) Hope it not bother youPhotoperiod
Erm... I'd like to ask a question about configuration... but it's kind of complicate, I ask the internet but they just don't give me the answer I want, so I want to ask you personally. Would you give me your email so I can mail you directly the question? Mine is [email protected]Photoperiod
P
0

How silly I am... just wrong logic in interceptor. All is fine now!!

 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();
    }

In the original code:

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

This cause an infinitite Loop, because i'm not loged in, then userId always<0 then always redirect and never let through the ActLogin to Log in

Photoperiod answered 17/5, 2013 at 7:20 Comment(1)
Lol stupid code, younger me. Took 1 day to fix a simple logic lolol.Photoperiod

© 2022 - 2024 — McMap. All rights reserved.