简体   繁体   中英

Struts2 Ajax issue

The problem is the following. When user clicks on the link I load the data into a div, but when the session of the user is expired I need to redirect him to login page, or if the is external error I need to redirect the user to error page. The problem is, if for example my interceptor sees that the user is no longer in session and tries to redirect him to login page, the login page is again loaded in the div. the other elements on page remain. How can I make the page reload without JavaScript? I mean directly from struts.xml.

You need to send to your browser whether it is a new session or not.

One way.

In your interceptor add a header to indicate whether it is a new session. Then if you are using jquery you can bind the ajax complete and check the header for the attribute that its a new session. And finally on new session, do a location.reload.

your ajax for jquery would look something like this

    jQuery(document).ajaxComplete(function(e, xhr, settings) {

        var status = xhr.status;
        if(status == 200 ){
            var isAuth = xhr.getResponseHeader('_isnewsession');

            if((isAuth == '1')){
                alert('Your Session has timed out!');
                location.reload();
            }
        }
    });

Does that make sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM