I have a problem with h:selectManyListbox
, when the items are populated with POJO's and the noSelectionOption
is true (for h:selectManyListbox
with enums as items, it works as I expected).
Bean
@Named
@ViewScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<BaseDTO> availableItems = null;
private String[] selectedItems = null;
@PostConstruct
private void initialize() {
loadAvailableItems();
}
private void loadAvailableItems() {
availableItems = Arrays.asList(new BaseDTO("entityId", "entityDescription"), new BaseDTO(...), ...);
}
public List<BaseDTO> getAvailableItems() {
return availableItems;
}
public String[] getSelectedItems() {
return selectedItems;
}
public void setSelectedItems(String[] selectedItems) {
this.selectedItems = selectedItems;
}
}
BaseDTO
public class BaseDTO {
private String id;
private String description;
public BaseDTO(String id, String description) {
this.id = id;
this.description = description;
}
public String getId() {
return id;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseDTO other = (BaseDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
XHTML
<h:selectManyListbox value="#{myBean.selectedItems}" hideNoSelectionOption="false" size="4">
<f:selectItem itemValue="#{null}" itemLabel="--" noSelectionOption="true" />
<f:selectItems value="#{myBean.availableItems}" var="entry" itemValue="#{entry.id}" itemLabel="#{entry.description}" />
</h:selectManyListbox>
When I try to submit the page I always get Validation Error: Value is not valid
.
If I remove the hideNoSelectionOption
and the correponding <f:selectItem itemValue="#{null}" itemLabel="--" noSelectionOption="true" />
everything works fine, however I really would like to have this noSelectionOption
on my list.
I tried using OmniFaces SelectItemsConverter and even creating my own custom converter, but with no luck. No matter what I try I cannot overcome this validation error.
Meanwhile I found a not so nice workaround:
If my availableItems
variable is a Map<String, String>
instead of a List
:
private Map<String, String> availableItems = null;
and if I add a null entry to the map:
private void loadAvailableItems() {
List<BaseDTO> dtoList = Arrays.asList(new BaseDTO("entityId", "entityDescription"));
availableItems = dtoList.stream().collect(Collectors.toMap(BaseDTO::getId, BaseDTO::getDescription));
availableItems.put(null, "--");
}
then, everything works as expected, except the noSelectionOption
is not preselected on the page.
I this the expected component behaviour, or am I missing something?
Thanks in advance for your help!