点出 退出 旋钮,防止点击浏览的 后退 按钮仍然可以进系统的解决方案

点出 退出 按钮,防止点击浏览的 后退 按钮仍然可以进系统的解决方案

项目使用的struts2 框架,登陆完了之后,在浏览器的地址栏里显示的一直是:

http://IP:PORT/app-name/user!login

这样导致,当点击画面上的退出按钮通过form提交了 "user!logout"的请求后,还是可以打开系统首页,并且能够继续操作(因为 "user!login" 请求在画面回退的时候被重新发送了)

所以,在网上了查了一下,可以通过以下的方式来避免(准确地说,应该是在一定程度上避免这种情况)

重点是:location.replace() 方法的使用

 

    // logout
    $("#logout").click(function() {
        var res = window.confirm(msg.logoutConfirm);
        if (!res) {
            return false;
        } else {
            $.ajax({
                'url' : contextPath + '/user!logout',
                'type' : 'post',
                'async' : false,
                'complete' : function(xhr, status) {
                    window.top.location.replace(contextPath + "/logout.jsp");
                }
            });
        }
    });