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
Session 如何使JSF2.0中的会话无效?_Session_Jsf 2_Httpsession_Managed Bean_Session Scope - Fatal编程技术网

Session 如何使JSF2.0中的会话无效?

Session 如何使JSF2.0中的会话无效?,session,jsf-2,httpsession,managed-bean,session-scope,Session,Jsf 2,Httpsession,Managed Bean,Session Scope,在JSF2.0应用程序中,使会话无效的最佳方法是什么?我知道JSF本身不处理会话。到目前为止我能找到 private void reset() { HttpSession session = (HttpSession) FacesContext.getCurrentInstance() .getExternalContext().getSession(false); session.invalidate(); } 这个方法正确吗?有没有一种方法不去碰地板

在JSF2.0应用程序中,使会话无效的最佳方法是什么?我知道JSF本身不处理会话。到目前为止我能找到

private void reset() {
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
    session.invalidate();
}
  • 这个方法正确吗?有没有一种方法不去碰地板 ServletAPI
  • 考虑一个场景,其中一个
    @SessionScoped
    UserBean处理 用户的登录和注销。我在同一个bean中有这个方法。现在 完成必要的DB后调用
    reset()
    方法时 更新后,我的当前会话范围bean将发生什么变化?自从 甚至bean本身也存储在
    HttpSession
  • 首先,这个方法正确吗?有没有不接触ServletAPI的方法

    您可以使用使会话无效,而无需获取Servlet API

    @ManagedBean
    @SessionScoped
    public class UserManager {
    
        private User current;
    
        public String logout() {
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            return "/home.xhtml?faces-redirect=true";
        }
    
        // ...
    
    }
    

    我的当前会话作用域bean将发生什么?因为连bean本身都存储在HttpSession中

    它在当前响应中仍然可以访问,但在下一个请求中不再存在。因此,在invalidate之后触发重定向(新请求)是很重要的,否则您仍然显示来自旧会话的数据。重定向可以通过向结果中添加
    faces redirect=true
    来完成,就像我在上面的示例中所做的那样。发送重定向的另一种方法是使用

    但是,在这种情况下,它的使用是有问题的,因为使用导航结果更简单。

    前端代码是:

    public void logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    }
    
    <h:form>
    <h:commandLink action="#{userManager.logout()}">
           <span>Close your session</span>
    </h:commandLink>
    </h:form>
    
    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return "/login.xhtml?faces-redirect=true";  
    }
    

    @BalusC,
    ExternalContext#invalidateSession()
    HttpSession#invalidate()
    之间有什么区别?@Patrick:从功能上讲,没有什么区别。他们两个做的完全一样
    ExternalContext#invalidateSession()
    在封面下调用
    HttpSession#invalidate()
    (另请参见我答案中的javadoc链接)。从技术上来说,
    ExternalContext
    方法更好。基本上,您应该努力在任何与JSF相关的工件中实现零
    javax.servlet.*
    导入。SSL会话呢?有可能使它无效吗?因为我尝试了建议的方法,但没有成功
    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return "/login.xhtml?faces-redirect=true";  
    }