0

In my application, I would know when the session has expired by doing Ajax calls every 30 seconds, but the problem is that when I can controller method, HttpSession refresh and lastaccessedtime changed. So have you any idea how to know the status of the session without changing it's behaviour. I'm using Jquery and SpringMVC 2.5

@RequestMapping(value = "/consultForm.htm", params = "verifySession", method = RequestMethod.GET)
public ModelAndView verifySessionStatus(WebRequest webRequest, Model model, HttpServletRequest request, HttpServletResponse response) {
    boolean isValid = true;
    HttpSession session = request.getSession(false);
    if (session.getAttribute("user") == null) {
        isValid = false;
    }
    ModelAndView modelAndView = new ModelAndView("jsonView");
    model.addAttribute("sessionValid", isValid);
    modelAndView.addObject(model);
    return modelAndView;
}


$.ajax({
    url : "search.htm?verifySession=1",
    type : "GET",
    async : false,
    cache : false,
    success : function(data) {

        valid = data.sessionValid;
        if (!valid) {
            showPopinExpire = true;
            $("#dialog-session-expired").dialog("open");
        }
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(thrownError);
    } 
});

Even with request.isRequestedSessionIdValid(), everytime I call controller method, the session was refreshed.

Thank you

4

3 に答える 3

2

From your example, it seems you want a json response as you are doing this:

valid = data.sessionValid;

It is not the default dataType, so you need to specify that:

$.ajax({
    url : "search.htm?verifySession=1",
    type : "GET",
    async : false,
    cache : false,
    dataType: 'json' // added this
    // your rest of the code
于 2012-06-15T08:27:30.930 に答える
1

サーバーにリクエストを送信するたびに、セッションlastAccessedTimeが更新されます。

あなたができることは、あなたが望むたびにセッションにカスタム変数のタイムスタンプを保存することです。次に、ajaxを使用して、コントローラーのメソッドを呼び出して、オブジェクトに格納されているカスタムタイムスタンプを取得できます。AOPメソッドを使用してタイムスタンプを生成することをお勧めします。邪魔になりません。

幸運を !

于 2012-06-15T14:03:02.580 に答える
1

You can use timer in Javascript to count the time for session to expire, and have register ajaxSend handler to reset the timer.

于 2012-06-15T08:26:13.690 に答える