在我的java ee应用程序中,我无法实现注销功能。当我尝试实现它时会发生这种情况: 我有一个header.xhtml,它有我的应用程序的标题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>
我该如何解决?
答案 0 :(得分:1)
如果您的loginBean是SessionScoped托管bean并且logout方法是该托管bean的方法,则使会话无效:
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" />
答案 1 :(得分:0)
尝试在托管bean中调用方法是错误的方法。你可能想要:
<h:commandLink action="#{loginBean.logoutAction()}" value="Logout" />
另外,你是如何登录的?如果您正在滚动自己的登录机制,那么使会话无效可能没问题,但如果您使用的是web.xml安全性约束,那么您应该使用Java EE 6编程登录API来实现servlet。