Problem with picklist (prefill & select)
jn.colin May 28, 2013 1:36 PMHi
I know this issue has been discussed several times, but I could not find a suitable solution for my problem.
I'm using RichFaces 4.3.2 with JSF 2.1.19. I have a picklist (see definition below) where I need to populate the select area with a list of value I extract from the DB.
<rich:panel styleClass="fullwidth" rendered="#{!adminProjectBean.currentProject.visibleToAll}"> <f:facet name="header"> <h:outputText value="Select users"></h:outputText> </f:facet> <rich:pickList sourceCaption="Available users" targetCaption="Authorized users" listWidth="165px" listHeight="100px" orderable="false" value="#{adminProjectBean.selectedUsers}" var="user"> <f:selectItems value="#{adminProjectBean.availableUsers}" /> <f:converter converterId="MyVisibilityConverter" /> <rich:column> #{user.firstName} </rich:column> <rich:column> #{user.lastName} </rich:column> </rich:pickList> </rich:panel>
I have a selectItems for the available values, loaded as:
availableUsers = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getUserDAO().getUsers();
Available values display properly.
The preselected list is loaded with:
List selection = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getUserDAO().getProjectUsers(currentProject.getId()); if ((selection == null) || (selection.size() == 0)) { currentProject.setVisibleToAll(true); } else { selectedUsers.addAll(selection); currentProject.setVisibleToAll(false); }
Both queries load objects of type UserTO. The problem is that the selected list (selectedUsers) contains the right records, but the picklist's right side is always empty. I've read all similar issues I could find, I did implement a Converter (see attached), I added an equals method to the UserTO object, but still, I can't get any item prefilled in the selected area.
@FacesConverter("MyVisibilityConverter") public class VisibilityConverter implements Converter { List<UserTO> userMap; /* * (non-Javadoc) * * @see * javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext * , javax.faces.component.UIComponent, java.lang.String) */ @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (userMap != null) { for (UserTO user : userMap) { if (user.getUserid() == Long.parseLong(value)) { System.out.println("getAsObject: " + user.getUsername()); return user; } } } else { Logger.getLogger(VisibilityConverter.class.getName()).log( Level.SEVERE, null, "map is empty"); } return null; } /* * (non-Javadoc) * * @see * javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext * , javax.faces.component.UIComponent, java.lang.Object) */ @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null) return null; System.out.println("getAsString " + ((UserTO) value).getUsername()); return Long.toString(((UserTO) value).getUserid()); } /** * @param selectedUsers */ public void setMap(List<UserTO> selectedUsers) { userMap = selectedUsers; } }
What is strange though is that if I copy an Object from the available list to the selected list (for instance: selectedUsers.add(availableUsers.get(0)), then, it gets properly displayed.
Could you please help?
-
VisibilityConverter.java.zip 1.5 KB