java - La acción Struts2 no pudo ejecutarse cuando redirigía(con un interceptor) una acción a otra
interceptorstack (2)
Envío una solicitud a ActLand
, luego intercept()
, si no estoy conectado, luego lo redirecciono a 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>
El 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();
}
El 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)
Lo siento, creo que hice todo lo que me habían ordenado, pero las cosas son complicadas ...
Agregue <interceptor-ref name="defaultStack"/>
a loginStack
después del interceptor de login
. Ayudará a resolver el problema con el resultado de redireccionamiento y no solo.
Que tonto soy ... solo lógica equivocada en el interceptor. ¡Todo está bien ahora!
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();
}
En el código original:
if (userId < 0) {
return "loginRedirect";
} else if (!(action instanceof ActLogin)) {
return "loginRedirect";
}
Esto causa un bucle infinito, porque no estoy conectado, entonces userId siempre <0, luego siempre se redirige y nunca dejo pasar el ActLogin para iniciar sesión