jsf - Using converter for custom objects in selectCheckboxMenu doesn't work -


this question has answer here:

i trying use converter custom objects, used in primefaces' selectcheckboxmenu.

this jsf part:

<p:outputlabel value="#{msg.cars}: " for="cars" /> <p:selectcheckboxmenu id="cars"     value="#{controller.selected.cars}"     converter="carconverter" label="#{msg.cars}"     filter="true" filtermatchmode="startswith"     panelstyle="width:200px">     <f:selectitems         value="#{controller.available.cars}" />     <f:converter converterid="carconverter" /> </p:selectcheckboxmenu> 

and converter:

@facesconverter("carconverter") public class carconverter implements converter {      @override     public object getasobject(facescontext context, uicomponent component, string newvalue) {         return null;     }      @override     public string getasstring(facescontext context, uicomponent component, object object) {         if (object == null) {             return "";         }         if (object instanceof car) {             car car = (car) object;             string name = car.getname();             return name;         } else {             throw new converterexception(new facesmessage(object + " not valid car"));         }     } } 

getasstring() returns correct string. selectcheckboxmenu still lists objects , not strings.

am missing something?

if need show car name in checkboxmenu label have use selectitems' itemlabel attribute

<p:outputlabel value="#{msg.cars}: " for="cars" /> <p:selectcheckboxmenu id="cars"     value="#{controller.selected.cars}"     converter="carconverter"     filter="true" filtermatchmode="startswith"     panelstyle="width:200px">     <f:selectitems value="#{controller.available.cars}" var="car" itemlabel="#{car.name}" itemvalue="#{car}"/> </p:selectcheckboxmenu> 

btw don't declare 2 converters (one via converter attribute , other via f:converter), , override correctly getasobject method (it's needed during apply request values phase). check docs details


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -