我在Java Spring Framework中有一个基于Ajax的选项卡切换应用程序,为此我必须处理“后退”按钮方案。通过下面的指定代码成功完成了此操作。现在所面对的效果是首页加载,而不是为特定历史记录选择的选项卡显示的选项卡切换跳转。如果有人对此表示帮助,将非常感谢。

var innerTab = "";
function navigateAjaxHtmlSetup(target,response){
        if (typeof target == 'undefined')
        target = "#ajax-container";
        $(target).html(response);
        $("input[check-with-select]:checked").parent("label").parent("td").nextUntil(":last").find('select').attr('required', 'required');
        $(target).focus();
        $("#ajax-loader").hide();
}
function pushStateToHistory(){
    var history = window.history;
    var location = window.location.pathname;
    history.pushState({}, "GHFP", location+activeTabs());
}

function anchorAjaxCall(self,url,state){
        $.ajax({
            type: 'GET',
            url: url,
            dataType: "html",
            success: function (response) {
                var target = self.attr('targeto');
                navigateAjaxHtmlSetup(target,response);
                if (state)
                    pushStateToHistory();
//              if(innerTab != ""){
//                  var anchor  = $("a#"+innerTab);
//                  innerTab = "";
//                  anchorAjaxCall(anchor,anchor.attr('href'));
//              }
            }
        });
}

function activeTabs(){
    selectedTabList = $(".tabs-nav a.active");
    var activeURL;
    if (selectedTabList.length > 0){
        activeURL="#";
        selectedTabList.each(function(index,el){
            activeURL +="/"+$(el).attr('id');
        });
    }else{
        activeURL="";
    }
    return activeURL;
}

function urlAjaxNavigationHandling(hash){
    if (hash.indexOf('/')!=-1)
    {
        var selectedTabList = hash.substring(1,hash.length).split('/');
        var anchor  = $("a#"+selectedTabList[1]);
        anchorAjaxCall(anchor,anchor.attr('href'),false);
        if (selectedTabList.length > 1)
            innerTab = selectedTabList[2];
    }
}
window.onpopstate = function(e) {
    urlAjaxNavigationHandling(window.location.hash);
};


尽管我的问题与Spring或Java根本无关,但只要有人用谷歌实现Ajax基本应用程序,我就将其指定为标签。这可能对他有帮助,因为我在Ruby on Rails中未在Spring Framework中找到对JQuery的任何内置支持。

最佳答案

Spring Framework不像Rails,Grails接近Rails,但是使用Groovy。

解决问题的方法基本上是使用浏览器的历史记录,因为您已经在尝试。我可以添加的唯一想法是,您应该查看HTML5历史记录API http://diveintohtml5.info/history.html,并考虑使用像http://backbonejs.org/这样的js框架,该框架已经支持路由器。

08-18 12:03