gpt4 book ai didi

java - 托管 Bean 注销不起作用

转载 作者:行者123 更新时间:2023-12-01 15:20:48 25 4
gpt4 key购买 nike

在我的 java ee 应用程序中,我无法实现注销功能。这就是当我尝试实现它时发生的情况:我有一个 header.xhtml,其中包含我的应用程序的 header css 部分:header.xhtml:(注销代码)

<div class="userid-link"><img src="images/app.png" alt=""/><p><a href="#{loginBean.logoutAction()}">Logout</a></p></div>

注销代码:loginBean.java

    public String logoutAction()
{
HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
try{
HttpSession session=req.getSession();
session.invalidate();
// req.logout();
}
catch(Exception e)
{

}
return"equityVolume.xhtml";
}

错误:

SEVERE: Error Rendering View[/ClientTemplate/userWatch.xhtml]
javax.el.ELException: /ClientTemplate/userWatch.xhtml @44,62 value="#{watchBean.ut}": java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)

...
INFO: Exception when handling error trying to reset the response.
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
at...

主页加载正确,但是当我尝试登录时,userWatch.xhtml 未正确呈现,我收到上述错误,CSS 也未应用。

watchBean.java

 public List<UserTrack> getUt() {
HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpSession session=req.getSession();// debugged and found that the session is null, this methos executes after login i.e. on the userWatch.xhtml that is redirected after login from homePage
this.uname=(String)session.getAttribute("uname");
ut=getAllUserTrack(uname);
return ut;
}

当我从 header.xhtml 中删除 logOutAction 方法调用时,一切正常,除了注销时出现 viewExpired 错误:

<div class="userid-link"><img src="images/app.png" alt=""/><p><a href="#/homePage">Logout</a></p></div>

如何解决?

最佳答案

如果您的 loginBean 是 SessionScoped 托管 Bean 并且注销方法是该托管 Bean 的方法,则使 session 无效:

public void logout() {
// Invalidate session of a sessionscoped managed bean
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
try {
// Redirect to page you want after logout
FacesContext.getCurrentInstance().getExternalContext().redirect("<INSERT HERE THE PAGE YOU WANT TO REDIRECT(just the name like 'homepage')");

} catch (IOException ex) {
Logger.getLogger(TravelerSession.class.getName()).log(Level.SEVERE, null, ex);
}

}

您可以在该方法上重定向到您想要的页面,也可以返回您想要访问的页面的名称。我觉得用bean的方法来做比较安全。

在网页上你应该有这样的内容:

<h:commandButton class="btn btn-info" action="#{loginBean.logout}" value="Log out" />

关于java - 托管 Bean 注销不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961480/

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