Why it doesn't show my logout message?
Because you've assigned it as a property of a session scoped bean and are then invalidating the session and sending a redirect. The invalidation of the session will destroy the session scope and all objects stored in it, including session scoped managed beans. The redirect thereafter will create a brand new request and session and all session scoped managed beans will be newly recreated, with all properties set to default.
There are 2 problems with this approach:
- Using
<h:outputText>
instead of<h:message(s)>
to display the message(s). - Storing the message in the session scope instead of the flash scope.
To solve your problem, use <h:message(s)>
to display the message(s) and use FacesContext#addMessage()
or, as you're already using OmniFaces, its Messages
utility class, to add a message to the faces context. You can use <h:messages globalOnly="true">
to display only global messages (i.e. those with a null
client ID). You can use Flash#setKeepMessages()
to tell JSF to store all messages of the current request in the flash scope instead.
Thus, all in all, this should do:
<h:messages id="messages" globalOnly="true" />
with
@ManagedBean
@RequestScoped // Doesn't need to be session scoped for the particular task.
public class UserManager {
public void logout() throws Exception {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have been logged out successfully");
Faces.invalidateSession();
Faces.redirect("login.xhtml");
}
}
For the interested, here's how it would look like using "plain JSF":
public void logout() throws Exception {
SecurityUtils.getSubject().logout();
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("You have been logged out successfully"));
ExternalContext externalContext = context.getExternalContext();
externalContext.getFlash().setKeepMessages(true);
externalContext.invalidateSession();
externalContext.redirect("login.xhtml");
}
Important note is that the flash scope is severly broken in older Mojarra versions. You need a minimum of 2.1.27 to get rid of all those problems.