0

我有一个拦截器,如果会话无效,我会在其中重定向到主页。我想使用 window.location 相同,但给出错误。

我的拦截器是:

@Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        if ((null == request.getSession(false))
                || (null == request.getSession(false).getAttribute(
                        "user"))) {
            System.out.println("user logged out...");
            ScriptEngineManager factory = new ScriptEngineManager();
            ScriptEngine engine = factory.getEngineByName("JavaScript");

            engine.eval("window.location='logout.do'");
        return false;
        }
        return super.preHandle(request, response, handler);
    } 

错误 :

   javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
        at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
        at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
        at com.snstech.mocktest.util.MyInterceptor.preHandle(MyInterceptor.java:23)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:761)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

或者他们是否有任何其他方式来做同样的事情......

谢谢,

4

2 回答 2

0

我认为这是无法做到的,我的意思是,使用 Java 中的 JS 重定向到页面。无论如何,这没有任何意义。

你应该改变策略。在这里,您可以找到一种方法来使用 Struts 和这里的 Spring。如果您对更多解决方案感兴趣,请咨询 Google。

于 2012-06-25T09:40:22.527 回答
0

ScriptEngineManager在服务器端运行脚本。window是一个客户端对象,你不能从服务器访问它。

如果您想这样做,您可以发送重定向,请参阅HttpServletResponse sendRedirect Permanent

于 2012-06-25T10:01:16.747 回答