我想在 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
。为什么会这样?FacesContext
在我的 servlet 中这个 AJAX 调用期间我看不到吗?如果以上是不可能的,有人可以建议任何其他方法吗?