无法使用java代码中的window.location加载新页面

时间:2012-06-25 08:11:28

标签: javascript spring javascript-engine

我有一个拦截器,如果会话无效,我将在其中重定向到主页面。 我想使用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)

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

感谢,

2 个答案:

答案 0 :(得分:0)

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

你应该改变策略。 Here,你可以找到一种方法来使用Struts和here来识别Spring。如果您对更多解决方案感兴趣,请询问Google。

答案 1 :(得分:0)

ScriptEngineManager在服务器端运行脚本。 window是客户端对象,您无法从服务器访问它。

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