2 Replies Latest reply on Dec 13, 2011 4:15 AM by subaochen

    how to access produced field in EL?

    subaochen

      hi all,


      I have a produce method like this:




      @Named
      @SessionScoped
      public class Login  implements Serializable{
      ...
              @Produces
              @LoggedIn
              @SessionScoped
              public User getCurrentUser() {
                      return currentUser;
              }
      }





      currentUser will be setted when user logged in.and I want to access currentUser in EL like this:




      welcome <h:outputText value="#{currentUser.username}"/>!





      but can not work. But:




      welcome <h:outputText value="#{login.currentUser.username}"/>!





      just works!


      I don't know why


       #{currentUser.username}



      does't work?


      Thanks in advance!



        • 1. Re: how to access produced field in EL?
          ahhughes

          You do not have a bean or producer qualified by the name currentUser, but you do have one named login and as you point out, this works.


          Sorry I can't test this and I don't guarantee this will work but...


          Try adding a produces method that returns a User and is qualified by @Named(currentUser). This will just return the @LoggedIn User (i.e. currentUser) but it will qualify the el name currentUser.


                  @Produces
                  @Named("currentUser")
                  public User produceCurrentUserByElName(@LoggedIn User currentUser) {
                          return currentUser;
                  }
          



          Fingers crossed it works for you :)

          • 2. Re: how to access produced field in EL?
            subaochen

            Thanks Andrew! yes, missing @Named for currentUser, this will work:





                    @Produces
                    @LoggedIn
                    @SessionScoped
                    @Named
                    public User getCurrentUser() {
                            return currentUser;
                    }