-
Available since OmniFaces 1.5
The omnifaces.ListConverter
is intented for use in specialized selection components which doesn't use SelectItem
s as the source for their selectable items, but work directly via a List
of entities, and therefore the SelectItemsConverter
isn't usable on them.
This converter allows you to populate a selection component with complex Java objects and have Faces convert those automatically back without the need to provide a custom converter which may need to do the job based on possibly expensive service/DAO operations. This converter automatically converts based on the Object.toString()
of the selected item.
Usage
This converter is available by converter ID omnifaces.ListConverter
and should be used in combination with <o:converter>
in order to be able to pass the List
source to it, which it can use for conversion. Here's a basic usage example with PrimeFaces <p:pickList>
, which is one of the few select components which doesn't use SelectItem
s as the source, but work directly via a List
.
<p:pickList value="#{bean.dualListModel}" var="entity" itemValue="#{entity}" itemLabel="#{entity.someProperty}">
<o:converter converterId="omnifaces.ListConverter" list="#{bean.dualListModel.source}" />
</p:pickList>
Since OmniFaces 4.5 it's also available by <o:listConverter>
tag.
<p:pickList value="#{bean.dualListModel}" var="entity" itemValue="#{entity}" itemLabel="#{entity.someProperty}">
<o:listConverter" list="#{bean.dualListModel.source}" />
</p:pickList>
Make sure that your entity has a good toString()
implementation
For detail, refer the javadoc of SelectItemsConverter
and substitute "SelectItemsConverter
" by "ListConverter
" and "SelectItemsIndexConverter
" by "ListIndexConverter
".
The demo below shows the PrimeFaces
<p:pickList>
component, which is a famous example of a component that does not use select items.
Another example of such a PrimeFaces component is the
<p:autoComplete>
.
<p>
The demo below shows the PrimeFaces
<a href="https://primefaces.org/showcase/ui/data/pickList.xhtml"><code><p:pickList></code></a>
component, which is a famous example of a component that does not use select items.
</p>
<h:form>
<p>
<b>Choose a city, then press Select</b>:
<p:pickList value="#{selectListBean.model}" var="entity" itemValue="#{entity}" itemLabel="#{entity.value}">
<o:converter converterId="omnifaces.ListConverter" list="#{selectListBean.fullList}" />
</p:pickList>
</p>
<p>
<h:commandButton value="Select">
<f:ajax execute="@form" render="@form"/>
</h:commandButton>
<b>Selected items</b>:
<ui:repeat value="#{selectListBean.model.target}" var="entity" varStatus="loop">
#{entity.value}#{loop.last ? '' : ', '}
</ui:repeat>
<h:outputText value="[no selection made]" rendered="#{empty selectListBean.model.target}"/>
</p>
</h:form>
<p>
Another example of such a PrimeFaces component is the
<a href="https://primefaces.org/showcase/ui/input/autoComplete.xhtml"><code><p:autoComplete></code></a>.
</p>
<h:form>
<p:autoComplete value="#{autoCompleteBean.selectedEntity}" completeMethod="#{autoCompleteBean.autoComplete}"
var="entity" itemValue="#{entity}" itemLabel="#{entity.value}">
<o:converter converterId="omnifaces.ListConverter" list="#{autoCompleteBean.availableEntities}" />
</p:autoComplete>
<p>
<h:commandButton value="Select">
<f:ajax execute="@form" render="@form"/>
</h:commandButton>
<b>Selected item</b>: #{autoCompleteBean.selectedEntity.value}
<h:outputText value="[no selection made]" rendered="#{empty autoCompleteBean.selectedEntity}"/>
</p>
</h:form>
package org.omnifaces.showcase.converters;
import static java.util.Arrays.asList;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Named;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.showcase.model.ExampleEntity;
import org.primefaces.model.DualListModel;
@Named
@ViewScoped
public class SelectListBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<ExampleEntity> fullList;
private DualListModel<ExampleEntity> model;
@PostConstruct
public void init() {
fullList = asList(
new ExampleEntity(1l, "Amsterdam"),
new ExampleEntity(2l, "Frankfurt"),
new ExampleEntity(3l, "Berlin")
);
model = new DualListModel<>(
new ArrayList<>(fullList),
new ArrayList<ExampleEntity>()
);
}
public List<ExampleEntity> getFullList() {
return fullList;
}
public DualListModel<ExampleEntity> getModel() {
return model;
}
public void setModel(DualListModel<ExampleEntity> model) {
this.model = model;
}
}
package org.omnifaces.showcase.converters;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import jakarta.inject.Named;
import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.showcase.model.ExampleEntity;
@Named
@ViewScoped
public class AutoCompleteBean implements Serializable {
private static final long serialVersionUID = 1L;
private ExampleEntity selectedEntity;
private List<ExampleEntity> availableEntities;
public List<ExampleEntity> autoComplete(String query) {
availableEntities = new ArrayList<>();
for (long i = 0; i < 5; i++) {
availableEntities.add(new ExampleEntity(i, query + i));
}
return availableEntities;
}
public ExampleEntity getSelectedEntity() {
return selectedEntity;
}
public void setSelectedEntity(ExampleEntity selectedEntity) {
this.selectedEntity = selectedEntity;
}
public List<ExampleEntity> getAvailableEntities() {
return availableEntities;
}
}
package org.omnifaces.showcase.model;
import java.io.Serializable;
public class ExampleEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String value;
public ExampleEntity() {
//
}
public ExampleEntity(Long id, String value) {
this.id = id;
this.value = value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object other) {
return (other instanceof ExampleEntity) && (id != null)
? id.equals(((ExampleEntity) other).id)
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (this.getClass().hashCode() + id.hashCode())
: super.hashCode();
}
@Override
public String toString() {
return String.format("ExampleEntity[%d, %s]", id, value);
}
}