Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a custom converter for <p:pickList>

Tags:

How can I write a custom converter when working with PrimeFaces components that use a list of POJO? My particular problem is with <p:pickList>

<p:pickList converter="????" value="#{bean.projects}" var="project"                               itemLabel="#{project.name}" itemValue="#{project}"> 

Without a converter I get java.lang.ClassCastException because JSF sets the submitted values with unconverted java.lang.String submitted values.

like image 612
Thang Pham Avatar asked Sep 01 '10 19:09

Thang Pham


2 Answers

It's possible, whithout other database access, but i don't know the best way. I use a very specific converter, works only for picklist. Try this:

@FacesConverter(value = "primeFacesPickListConverter")public class PrimeFacesPickListConverter implements Converter { @Override public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {     Object ret = null;     if (arg1 instanceof PickList) {         Object dualList = ((PickList) arg1).getValue();         DualListModel dl = (DualListModel) dualList;         for (Object o : dl.getSource()) {             String id = "" + ((Project) o).getId();             if (arg2.equals(id)) {                 ret = o;                 break;             }         }         if (ret == null)             for (Object o : dl.getTarget()) {                 String id = "" + ((Project) o).getId();                 if (arg2.equals(id)) {                     ret = o;                     break;                 }             }     }     return ret; }  @Override public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {     String str = "";     if (arg2 instanceof Project) {         str = "" + ((Project) arg2).getId();     }     return str; } 

and Picklist:

<p:pickList converter="primeFacesPickListConverter" value="#{bean.projects}" var="project"                          itemLabel="#{project.name}" itemValue="#{project}"> 

Work's for me, improvements is necessary.

like image 142
Braully Rocha Avatar answered Sep 18 '22 12:09

Braully Rocha


After research on how to write custom converter, here is the solution.
1. create a Java Class that implement 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. Register your custom converter in faces-config.xml

<converter>     <converter-id>projectConverter</converter-id>     <converter-class>org.xdrawing.converter.ProjectConverter</converter-class> </converter> 

3. So now inside Primefaces component, u just do converter="projectConverter". Note that projectConverter is the <convert-id> I just created. So to solve my problem above, I do this:

<p:pickList converter="projectConverter" value="#{bean.projects}" var="project"                              itemLabel="#{project.name}" itemValue="#{project}"> 
like image 44
Thang Pham Avatar answered Sep 21 '22 12:09

Thang Pham