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 是否需要valueUnbound中的会话变量?_Jsf 2_Session Variables_Session Timeout_Spring Webflow 2 - Fatal编程技术网

Jsf 2 是否需要valueUnbound中的会话变量?

Jsf 2 是否需要valueUnbound中的会话变量?,jsf-2,session-variables,session-timeout,spring-webflow-2,Jsf 2,Session Variables,Session Timeout,Spring Webflow 2,在会话即将到期之前,我正在使用HttpSessionBindingListener的valueUnbound方法释放锁(数据库中的一个条目): @Override public void valueUnbound(HttpSessionBindingEvent event) { String user = (String) event.getSession().getAttribute("currentUsr"); removeLock(user); } 设置锁后,我将用户名设置为会

在会话即将到期之前,我正在使用HttpSessionBindingListener的valueUnbound方法释放锁(数据库中的一个条目):

@Override

public void valueUnbound(HttpSessionBindingEvent event) {
  String user = (String) event.getSession().getAttribute("currentUsr");
  removeLock(user);
}
设置锁后,我将用户名设置为会话变量。
我需要在我的移除锁方法中使用这个“用户名”。但是getAttribute正在引发异常:

java.lang.IllegalStateException:getAttribute:会话已无效

我需要帮助获取会话变量??或者有没有其他方法获取用户名


否,因为会话已无效

尽管我找到了解决方案,但我还是通过servlet上下文在 valueBound方法并通过event.getSession().getServletContext().getAttribute(“cUser”)获取它

它很好用。谢谢你,EJP


我得到了你的观点EJP,你是对的,我把它变得复杂了,我可以从event.getValue()中得到它+谢谢你的回答

虽然我找到了解决方案,但我正在valueBound方法中通过servlet上下文设置属性,并通过:
event.getSession().getServletContext().getAttribute(“cUser”)

所以。。您正在应用程序范围中存储会话范围的数据。您是否意识到,通过这种方式,Web应用程序的所有访问者都可以共享数据?然后,访问者X将看到访问者Y设置的属性,该属性在稍后访问了网站。这只会让问题变得更糟

无论如何,对于具体的问题,正如异常消息试图告诉您的那样,会话已在此时失效。有两种方法可以解决此问题:

  • 使
    currentUsr
    成为实现
    httpsessionbindingstener
    的类的属性,这样您就不需要将其作为一个独特的会话属性来获取

  • 改用
    HttpSessionListener
    sessionDestroyed()
    方法在失效之前被调用,因此您仍然可以访问所有属性


  • 请不要将答案用于评论。谢谢。我实现了第一个,它工作顺利。谢谢你的解释。今天学到了新东西:)。谢谢@Balusc不客气。既然你是新来的,请别忘了在答案上标注“已接受”,这对解决问题有很大帮助。另见