本文介绍了关闭标签时结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有Java EE和JSF 2.0的应用程序中,我希望当用户关闭应用程序的选项卡时,会话结束.如果该用户在应用程序中打开了另一个选项卡,则不应关闭该另一个选项卡的会话.这样就不会打扰应用程序的管理.

In my application with Java EE and JSF 2.0 I would like that when the user closes the tab of the application the session ends. If this user has opened another tab in the application then the session of this other tab should not be closed. This so to not disturb the management of the application.

我要结束会议:

public String deconnecter() {
    //invalidate user session
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
    session.invalidate();
    return "login";
}

推荐答案

可能为时已晚,但是由于我在网上看到许多与此相关的问题,因此我可以将解决方案放在这里,在chrome和firefox上进行了测试(尽管是所有最新版本)

Probably this is too late, but since i have seen a number of questions on the web pertaining to this, i may just put my solution here, that i have tested on chrome and firefox (all latest editions though)

在设置上:一种更优雅的解决方案是使用过滤器,但我使用了prerender事件(我的整个网站使用单个模板,因此我可以跟踪何时加载每个页面,并且我在on上指定了prerender事件模板)

On set: a more elegant solution is to use a filter, but i have used a prerender event (Using single template for my entire website, hence i can track when every page is loaded, and i have specified the prerender event on the template)

    window.onbeforeunload = function(){
      //log out of the server too.
      var xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      var url = '/context/index.xhtml?onunload=true';
      xmlhttp.open('GET', url, true);
      xmlhttp.send();
    }
     window.onload = function(){
      console.log('window name: '+window.name);
      if(window.name != '#{controller.sessionId}'){
        window.name = '#{controller.sessionId}';
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = '/context/index.xhtml?onload=true';
        xmlhttp.open('GET', url, true);
        xmlhttp.send();
      }
    }
    //Called when user logs out.
    function onLogout(){
      console.log('logging out: '+window.name);
      window.name = '';
      console.log('logged out: '+window.name);
    }

服务器端代码:

在对prerender事件渲染视图之前调用此方法

Server Side Code:

This method is called before the view is rendered on the prerender event

public void trackUserTabs() {
String onload = Controller.getParameter(ONLOAD_ID);
if (onload != null && onload.trim().equals("true")) {
  openedTabs++;
  System.err.println("onload: " + controller.getCurrentPrinciple() + "..........." + openedTabs);
}
String onunload = Controller.getParameter(ONUNLOAD_ID);
if (onunload != null && onunload.trim().equals("true")) {
  openedTabs--;
  System.err.println("onunload: " + controller.getCurrentPrinciple() + ".............." + openedTabs);
}
if (openedTabs <= 0 && controller.getCurrentProfile() != null) {
  /**
   * All tabs are closed, log out current user.
   */
  controller.logoutCurrentProfile();
}

}

ajax请求的javascript代码由Stackoverflow中的答案提供.

The javascript code for ajax request is courtesy of an answer in Stackoverflow.

这篇关于关闭标签时结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:01