我正在上传一些使用Java编写的具有上传功能的图像。上传完成后,会显示一个gif显示进度条。现在,当会话超时时,我将重定向到登录页面。

    if (StringUtils.isNotBlank(request.getQueryString())) {
        queryString = request.getServletPath() + "?" + request.getQueryString();
    } else {
        queryString = request.getServletPath();
    }

    LOGGER.debug("Query String:" + queryString);
    if (!StringUtils.equalsIgnoreCase(StringUtils.substringAfterLast(queryString, ApplicationConstants.DOT),
                    ApplicationConstants.JSON)) {
        request.getSession().setAttribute(ApplicationConstants.QUERY_STRING, queryString);
    }
    try {
        LOGGER.error("In navigateToLandingPage. Redirecting to Failure View:" + this.getFailureView());
        response.sendRedirect(request.getContextPath() + "/" + this.getFailureView());
       // response.setHeader("Refresh", "5; URL=http://XXXX/login.htm");
    } catch (IOException e) {
        throw new BusinessException("Exception in redirecting", "-1", this.getClass().getCanonicalName(), e);
    }


当我在浏览器中分析网络时,我可以看到状态为302的响应,并且该位置具有预期的URL,但有些响应在未得到处理并停留在进度条屏幕上的方式。
如果我手动刷新卡住的页面,我会在登录页面上定向,我试图添加
response.setHeader(“刷新”,“ 5; URL = http://lXXXXX/login.htm”);下面的重定向,但没有运气。
谁能帮我解决这个问题。

有一种解决方法,而不是发送302,而是发送901,我在js处检查并指示登录正常,但我打算使用重定向。

最佳答案

那么,使用JS而不是使用Web浏览器异步触发了请求?换句话说,您正在尝试重定向ajax请求?

如果您要发送重定向作为对ajax请求的响应,则XMLHttpRequest将默认遵循该重定向。即它将ajax请求重新发送到该URL。这显然不是您的意图。您需要同步重定向,而不是异步重定向。

规范的方法是以某种方式指示ajax引擎,它需要在给定的URL上执行window.location调用。如果您使用JSON或XML作为数据格式,则只需添加一条特殊指令,然后您的ajax引擎即可对其进行检查。

例如。如果是JSON:

response.getWriter().printf("{redirect:\"%s\"}", url);




var response = fireAjaxRequestAndGetResponseAsJsonSomehow();

if (response.redirect) {
    window.location = response.redirect;
    return;
}

// Usual process continues here.

关于java - 重定向没有发生,尽管位置具有正确的URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30889875/

10-13 02:39
查看更多