Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 使用JSF2.0 Mojarra和JQuery使会话无效_Jsf 2 - Fatal编程技术网

Jsf 2 使用JSF2.0 Mojarra和JQuery使会话无效

Jsf 2 使用JSF2.0 Mojarra和JQuery使会话无效,jsf-2,Jsf 2,在JSF2.0中,我希望在窗口关闭期间使会话无效。因此,我编写了以下代码来实现这一点: var preventUnloadPrompt; var messageBeforeUnload = "my message here - Are you sure you want to leave this page?"; $('a').live('click', function() { preventUnloadPrompt = true; }); $('form').live('s

在JSF2.0中,我希望在窗口关闭期间使会话无效。因此,我编写了以下代码来实现这一点:

 var preventUnloadPrompt;
 var messageBeforeUnload = "my message here - Are you sure you want to leave this page?";
 $('a').live('click', function() {
    preventUnloadPrompt = true;
 });
 $('form').live('submit', function() {
    preventUnloadPrompt = true;
 });
 $(window).bind("beforeunload", function(e) {
    var rval;
    if (preventUnloadPrompt) {
        return;
    } else {
        // return messageBeforeUnload;
        doInvalidate();
    }
    return rval;
 });

function doInvalidate()
{
     $.ajax({
         url: "http://localhost:8080/MyPrj/SessionTimeout",
         type: 'GET'
     });
 }
我的servlet如下所示:

 public class SessionTimeout extends HttpServlet {
    .....
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.err.println("IN SESSION TIMEOUT GET!!!");
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
   }
  .....
}

在启动我的第一个JSF2.0页面(此时FacesContext必须已经初始化)之后,我尝试关闭窗口。我可以看到我的
SessionTimeout
servlet正在被调用,但是
FacesContext.getCurrentInstance().getExternalContext().invalidateSession()正在抛出
NullPointerException
。为什么会这样?在我的servlet中调用AJAX时,我不能看到
FacesContext
?如果上述方法不可行,可以建议其他方法吗?

FacesServlet
创建
FacesContext
,因此只有当
FacesServlet
提供请求时才可用。换句话说,它只在JSF工件(如托管bean、阶段侦听器等)中可用,但在独立于JSF调用的“普通”servlet中肯定不可用

只需使用标准的ServletAPI方法,就像JSF使用的“隐藏”(你知道,JSF是一个基于servlet的MVC框架,检查
FacesServlet
是一个servlet!)。要使会话无效,只需执行
ExternalContext#invalidateSession()
正在执行的操作

request.getSession().invalidate();

是的,没错。谢谢你的回答。