selectManyListbox and SelectItem error
jbosskiki Mar 19, 2007 11:37 AMHi,
I'm trying to use two selectManyListBox to assign a list of roles to a user.
<h:selectManyListbox value="#{rolemanager.source}" converter="#{rolemanager.converter}" size="5"> <f:selectItems value="#{rolemanager.items}" /> </h:selectManyListbox> <h:commandButton value="add" action="#{rolemanager.add}" /> <h:commandButton value="remove" action="#{rolemanager.remove}" /> <h:selectManyListbox value="#{rolemanager.selected}" converter="#{rolemanager.converter}" size="5"> <f:selectItems value="#{rolemanager.destination}" /> </h:selectManyListbox>
when I click on "Add", the list "source" in role manager is a list of Role and not a list of SelectItem, I'd like to know if it's a normal behaviour... I expect to have a List of SelectItem...
the Complte code is
RoleManager.java
@Stateful @Scope(SESSION) @Name("rolemanager") public class RoleManagerBean implements RoleManager { @In(value="agaetisDatabase") private EntityManager em; @EJB private CompanyFacade company; private List<Role> roles; private List<SelectItem> items; private List<SelectItem> source=new ArrayList<SelectItem>(); private List<SelectItem> destination=new ArrayList<SelectItem>(); private List<SelectItem> selected=new ArrayList<SelectItem>(); @Create public void loadData() { roles = company.findAllRoles(em); items=new ArrayList<SelectItem>(roles.size()); for(Role role : roles) items.add(new SelectItem(role, role.getRole())); } public List<Role> getRoles() { return roles; } public Converter getConverter() { return new Converter(){ public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException { if(value==null) return null; for(Role role : roles) if(role.getRole().equals(value)) return role; return null; } public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException { return value==null ? null : ((Role)value).getRole(); } }; } public List<SelectItem> getItems() { return items; } public void setItems(List<SelectItem> items) { this.items = items; } public List<SelectItem> getDestination() { return destination; } public void setDestination(List<SelectItem> destination) { this.destination=destination; } public List<SelectItem> getSource() { return source; } public void setSource(List<SelectItem> source) { this.source=source; } public void setSelected(List<SelectItem> selected) { this.selected = selected; } public List<SelectItem> getSelected() { return selected; } public void add() { destination.addAll(source); source.clear(); } public void remove() { source.addAll(destination); destination.clear(); } @Destroy @Remove public void destroy() { } public void processValueChange(ValueChangeEvent event) throws AbortProcessingException { System.out.println(event); } }
Role.java
@Entity @Table @Scope(SESSION) @NamedQueries({@NamedQuery(name = MappedQueries.ROLE_QUERY_FIND_BY_ROLE, query = "select r from Role r where r.role=:role") }) public class Role implements java.io.Serializable { private static final long serialVersionUID = 2; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(precision = 3, scale = 0) private short idrole; @NotNull @Length(max=70) @Column(unique = true, nullable = false, length = 70) private String role; @Length(max=200) @Column(length = 200) private String description; @ManyToMany(mappedBy = "roles") private List<User> users; public Role() { } public Role(String role) { this.role = role; } public Role(String role, String description) { this(role); this.description = description; } public short getIdrole() { return idrole; } public void setIdrole(short idrole) { this.idrole = idrole; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<User> getUsers() { if (users == null) users = new Vector<User>(0); return users; } public void setUsers(List<User> users) { this.users = users; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + idrole; return result; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Role other = (Role) obj; if (idrole != other.idrole) return false; return true; } @PrePersist public void format() { role = Formatter.captializeFirstLetter(role); } }
And the exception...
java.lang.IllegalArgumentException: Collection referenced by UISelectItems with binding '#{rolemanager.destination}' and Component-Path : {Component-Path : [Class: org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId: /credentials.xhtml][Class: javax.faces.component.html.HtmlForm,Id: credentials][Class: javax.faces.component.html.HtmlSelectManyListbox,Id: _id33][Class: javax.faces.component.UISelectItems,Id: _id34]} does not contain Objects of type SelectItem
Mickael