users@glassfish.java.net

Re: problem with _at_SessionScope annotation

From: Sahoo <Sahoo_at_Sun.COM>
Date: Thu, 08 Apr 2010 14:16:16 +0530

I don't understand something here. Even if user field does not keep its
value, since you call isAuthenticated(), the field should again get
populated, right?

Thanks,
Sahoo

glassfish_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>