"how to write a custom converter for <p:picklist>" Code Answer


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{

   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


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}">
By Alex Wiese on August 25 2022

Answers related to “how to write a custom converter for <p:picklist>”

Only authorized users can answer the Search term. Please sign in first, or register a free account.