jsp response.redirect导致jquery.mobile出现问题 [英] jsp response.redirect causes problems with jquery.mobile

查看:59
本文介绍了jsp response.redirect导致jquery.mobile出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是使用jsp页面和jquery.mobile构建的.我使用Java函数检查登录状态,并从这样的Bean中进行重定向(如果这样做不正确):

My application is build with jsp pages and jquery.mobile. I use a Java function to check login status and do a redirect when that's not OK from within a Bean like this:

public String getMenu(HttpSession session, HttpServletResponse response, MenuType menuType, CurrentPage currentPage)
{
    if (session.getAttribute("state") == LoginChecked.UNDEFINED))
    {
        response.sendRedirect("../index.jsp");

        return null;
    }
    else
    {
        ...

问题是到jindex.js的重定向是由jquery.mobile处理的,它是页面内容的ajax重新加载,而不是完整的重定向.

The problem is that the redirect to index.jsp is handled by jquery.mobile as an ajax reload of the content of the page, and not a complete redirect.

如何强制完全重新加载index.jsp页面的浏览器?

How can I force a complete reload of the browser of the index.jsp page?

-添加-

在html内,可以使用"rel"属性来解决此问题,因此我需要某种方法来从Java sendRedirect中使用此rel:

Within html this would be solved with the "rel" attribute like this, so I would need some kind of method to use this rel from within my Java sendRedirect:

<a href='../index.jsp' data-role='button' data-icon='plus' rel='external'>

推荐答案

我最近正在寻找解决类似问题的方法(在重定向AJAX请求时实际上是在重定向),在这里找到了一个很好的答案,我显然没有不会添加书签.但是,在为我自己成功实现它之后,我将分享基于该答案所采用的方法.

I was looking at solving a similar problem myself recently (actually redirecting when an AJAX request is redirected), and found a very good answer on here that I apparently didn't bookmark. However, having successfully implemented it for myself, I'll share the approach I took based on that answer.

第一步是识别AJAX请求,以便可以对它们进行不同于标准请求的处理.这可以通过检查X-Requested-With请求标头的值来完成.值XMLHttpRequest表示AJAX请求.对于标准请求,我们将像往常一样进行重定向.在处理AJAX请求而不是进行重定向的情况下,我所做的是设置响应标头以表示发生了重定向,并将其值设置为所需的URL重定向到.

The first stage was to identify AJAX requests so they can be handled differently to standard requests. This can be done by checking the value of the X-Requested-With request header; a value of XMLHttpRequest indicates an AJAX request. In the case of a standard request, we proceed with the redirect as normal. In the case of handling an AJAX request, rather than proceeding with the redirect, what I did is set a response header to signify that a redirect would have occurred, and set its value to the URL that I want to redirect to.

然后,我使用以下jQuery代码检查所有已完成的AJAX请求,并在适当时重定向:

Then, I used the following jQuery code to check all completed AJAX requests, and redirect when appropriate:

$(document).bind('ajaxComplete', function(event, xhr, options) {
    var redirectHeader = xhr.getResponseHeader('YourHeader');
    if(xhr.readyState == 4 && redirectHeader != null) {
        window.location.href = redirectHeader;
    }
});

这篇关于jsp response.redirect导致jquery.mobile出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆