4 Replies Latest reply on Jun 23, 2007 11:59 PM by Fijai Cairo

    selectOneMenu binding to session object

    Fijai Cairo Newbie

      I'm trying to bind the currentPersona attribute of the user session object to the selected item in the drop down. Most solutions for this issue on the forum seem to focus on db, persistence context, entity manager etc but my application doesn't use a db directly. I need to have the currentPersona update triggered onChange. If that won't work, an update button will suffice.

      I tried the ff because its similar to what worked for me in WO.(I can't shake my past :-)

      SelectOneMenu

      <h:selectOneMenu value="#{user.currentPersona}">
       <s:selectItems value="#{user.personaList}" var="per" label="#{per.selectLabel}" hideNoSelectionLabel="true" />
       </h:selectOneMenu>
      



      User Class
      @Name("user")
      @Scope(SESSION)
      public class User implements Serializable{
      
      @DataModel
      @Out
      private List<Persona> personaList;
      
      @DataModelSelection
      @Out(required=false)
      private currentPersona;
      
      ...
      
      //Mutators
      public Persona getCurrent Persona() {
       return current Persona;
      }
      
      
      public void setCurrentPersona(Persona selected Persona){
       log.info("Current Persona:", selectedPersona);
       this.current Persona = selectedPersona;
      }
      
      public List<Persona> getPersonaList() {
       return personaList;
      }
      
      



        • 1. Re: selectOneMenu binding to session object
          Pete Muir Master

          You need a JSF converter - JSF can't handle object in select boxes. Take a look at Rick Hightowers tutorials on Developer Works for a good intro to this.

          • 2. Re: selectOneMenu binding to session object
            Fijai Cairo Newbie

            Thanks Pete. Will check out Rick's tutorial. I actually went thru his facelets tutorial not too long ago.
            If only the valueChangeEvent or selectOne objects had an index attribute that I could read, I could use that same index to retrieve the the selected item from my list. Seems like an odd omission to me but then I'm new to JSF. I probably need to research it more.

            • 3. [solution]: selectOneMenu binding to session object
              Fijai Cairo Newbie

              If anyone else needs a solution, this is what I ended up doing.


              
              public class Avatar implements Serializable{
              
               private String nickName;
               private String style;
               private int index;
               /** Creates a new instance of Avatar */
               public Avatar() {
               }
              
               public Avatar(String nickName, String style, int index){
               this.style = style;
               this.nickName = nickName;
               this.index = index;
               }
              
              
               public String toString(){
               return this.index + "";
               }
              
               public String getNickName() {
               return nickName;
               }
              
               public String getStyle() {
               return style;
               }
              
               public String getSelectLabel(){
               return "***"+style + ":"+ nickName;
               }
              }
              
              
              
              @Name("avatarConverter")
              @Intercept(NEVER)
              @Converter
              public class AvatarConverter implements javax.faces.convert.Converter
              {
               @In User user;
               public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException
               {
               Integer i = new Integer(value);
               User user = (User) Contexts.getSessionContext().get("user");
               Avatar currentAvatar = user.getAvatarList().get(i.intValue());
               return currentAvatar;
               }
              
               public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException
               {
               return value + "";
               }
              }
              
              
              
              @Name("user")
              @Scope(SESSION)
              //@Startup
              public class User implements Serializable{
              
               @Logger private static Log log;
               private String userName;
               private Avatar currentAvatar;
               private List<Avatar> avatarList;
               /** Creates a new instance of User */
               public User() {
               }
              
               public User(String pUserName, List<Avatar> pAvatarList){
               this.userName = userName;
               this.avatarList = pAvatarList;
              
               if(avatarList.size()>0){
               this.currentAvatar = avatarList.get(0);
               } else {
               this.currentAvatar = new Avatar("Nick", "3d", 0);
               this.avatarList = new ArrayList();
               this.avatarList.add(currentAvatar);
               }
               }
              
               public List<Avatar> getAvatarList(){
               return this.avatarList;
               }
              
              
               @Create
               public void initialize(){
               System.out.println("User Initialized");
               }
              
               public void click(){
               log.info("User Click Invoked...");
               }
              
               public String getUserName() {
               return userName;
               }
              
               public void setUserName(String userName) {
               this.userName = userName;
               }
              
               public Avatar getCurrentAvatar() {
               return currentAvatar;
               }
              
               public void setCurrentAvatar(Avatar currentAvatar) {
               this.currentAvatar = currentAvatar;
               }
              
              }
              


              • 4. Re: selectOneMenu binding to session object
                Fijai Cairo Newbie

                The issue with this solution is that the converter is coupled to the User class and so will only work if the list being displayed by selectOneMenu is the one in the user class. It isn't an issue in my particular application though.