gpt4 book ai didi

session - 如何在 JSF 2.0 中使 session 失效?

转载 作者:行者123 更新时间:2023-12-03 05:43:40 26 4
gpt4 key购买 nike

在 JSF 2.0 应用程序中使 session 失效的最佳方法是什么?我知道JSF本身不处理 session 。到目前为止我能找到

private void reset() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false);
session.invalidate();
}
  1. 这个方法正确吗?有没有办法不碰ServletAPI?
  2. 考虑一个场景,其中 @SessionScoped UserBean 处理用户的登录-注销。我在同一个bean中有这个方法。现在当我完成必要的数据库后调用 reset() 方法时更新后,我当前的 session 作用域 bean 会发生什么情况?自从甚至bean本身也存储在HttpSession中?

最佳答案

Firstly, is this method correct? Is there a way without touching the ServletAPI?

您可以使用ExternalContext#invalidateSession()无需获取 Servlet API 即可使 session 失效。

@ManagedBean
@SessionScoped
public class UserManager {

private User current;

public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/home.xhtml?faces-redirect=true";
}

// ...

}
<小时/>

what will happen to my current session scoped bean? since even the bean itself is stored in HttpSession?

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

public void logout() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
}

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

关于session - 如何在 JSF 2.0 中使 session 失效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5619827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com