Dopo una ricerca su come scrivere un convertitore personalizzato, ecco la soluzione.
1. Creare una classe Java che implementano javax.faces.convert.Converter;
public class ProjectConverter implements Converter{
@EJB
DocumentSBean sBean;
public ProjectConverter(){
}
public Object getAsObject(FacesContext context, UIComponent component, String value){
return sBean.getProjectById(value);
//If u look below, I convert the object into a unique string, which is its id.
//Therefore, I just need to write a method that query the object back from the
//database if given a id. getProjectById, is a method inside my Session Bean that
//does what I just described
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return ((Project) value).getId().toString(); //--> convert to a unique string.
}
}
2. Registrare il convertitore personalizzato in faces-config.xml
<converter>
<converter-id>projectConverter</converter-id>
<converter-class>org.xdrawing.converter.ProjectConverter</converter-class>
</converter>
3. Così ora all'interno componente primefaces, u solo fare converter="projectConverter"
. Si noti che projectConverter
è il <convert-id>
appena creato. Quindi per risolvere il mio problema, faccio questo:
<p:pickList converter="projectConverter" value="#{bean.projects}" var="project"
itemLabel="#{project.name}" itemValue="#{project}">
fonte
2010-09-01 22:50:16
Domanda interessante. Sfortunatamente onestamente non lo so. Penso che dovresti fare una domanda e vedere cosa dice la community. Se lo fai in un post, per favore dammi il link. Voglio sapere anche questo. –