Hi guys,
I have a couple of jsf pages (composed using facelets). In the header part of the template I want to display "Login" if the user is not logged in yet and "Logout" if the user is logged in.
What I did so far was to create a backing bean as follows:
[i]@Named
@SessionScoped
public class UserManager implements Serializable {
private @EJB UserService userService;
private User user;
public User getUser() {
if (user == null) {
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
if (principal != null) {
user = userService.getUserByName(principal.getName()); // Find User by j_username.
}
}
return user;
}
public String getUserName() {
getUser();
if(user==null){
return "Guest";
}else{
return getUser().getFirstName()+" "+getUser().getLastName();
}
}
public String logout(){
HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
if(session!=null){
session.invalidate();
}
return "index";
}
public boolean isAuthenticated(){
return getUser()!=null;
}
}
[/i]
The important part in the header looks as follows:
[i]<h:outputText value="Welcome #{userManager.getUserName()}!"
rendered="#{userManager.isAuthenticated()}"/>
<h:outputLink value="login.xhtml"
rendered="#{!userManager.isAuthenticated()}">
Login
</h:outputLink>
<h:outputLink value="#{userManager.logout()}"
rendered="#{userManager.isAuthenticated()}">
Logout
</h:outputLink>
[/i]
The problem is that, even if I successfully login in the first page, when I get to the second page it looks like I'm not logged in. And indeed, while debugging, I noticed that the "user" field does not keep its value.
Can anybody suggest what am I doing wrong ?
Thank you,
Ionut
[Message sent by forum member 'sionut']
http://forums.java.net/jive/thread.jspa?messageID=395754