-
1. Re: selectOneMenu binding to session object
pmuir Jun 22, 2007 5:12 AM (in response to joeyxxx)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
joeyxxx Jun 22, 2007 10:12 AM (in response to joeyxxx)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
joeyxxx Jun 23, 2007 11:54 PM (in response to joeyxxx)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
joeyxxx Jun 23, 2007 11:59 PM (in response to joeyxxx)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.