Search code examples
spring-bootprimefacesjsf-2.2converterspicklist

p:PickList Custom JSF Converter in Spring Boot


I am currently getting

ClassCastException : com.singtel.eshop.converter.PickListConverter cannot be cast to javax.faces.component.UIComponent when using a custom JSF converter with p:pickList .

I am using : Primefaces 4, JSF 2.2.12 and Spring Boot 2.0.3

The following is my code

Converter

@FacesConverter("PickListConverter")
@Component("PickListConverter")
public  class PickListConverter  implements Converter  {

public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {

    PickList p = (PickList)component;
    DualListModel dl = (DualListModel)p.getValue();
    for (int i = 0; i < dl.getSource().size(); i++) {
        if (dl.getSource().get(i).toString().contentEquals(submittedValue)) {
            return dl.getSource().get(i);
        }
    }
    for (int i = 0; i < dl.getTarget().size(); i++) {
        if (dl.getTarget().get(i).toString().contentEquals(submittedValue)) {
            return dl.getTarget().get(i);
        }
    }
    return null;
}

public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
    PickList  p = (PickList) component;
    DualListModel dl = (DualListModel) p.getValue();
    //return  String.valueOf(dl.getSource().indexOf(value));
    return value.toString();
}

}

XHTML:

<p:dialog id="addDialog" header="Add Price Plan Scheme" widgetVar="addDlgl"
          position="600,100" modal="true" appendToBody="true" height="500" width="550">
    <h:form id="addForm">
     <p:selectOneMenu id="type"
                             value="#{bean.label}"
                             style="width:100px; margin-left:10px;">
                <f:selectItems value="#{bean.types}" var="s" 
                    itemValue="#{s.value}" itemLabel="#{s}" />
                <p:ajax update="addForm"
                        listener="#{bean.listener}"
                        global="false"/>
            </p:selectOneMenu>

  <h:panelGrid columns="1" cellpadding="0" style="padding-left:25px"
   rendered="#{bean.label == 'TEST'}">
                <p:pickList id="desc"
                            value="#{bean.list}"
                            style="width:250px !important;" var="data"
                            itemValue="#{data}"
                            itemLabel="#{data.desc}" 
                             responsive="true"
                            showSourceFilter="true" showTargetFilter="true"
                            filterMatchMode="contains"
                            binding="#{PickListConverter}"
                            >
                </p:pickList>
            </h:panelGrid>

Bean

 @Component
 @Scope(value="session")
 public class Bean{
private String label;
private DualListModel<DataBean> list;
}

DataBean

public class DataBean{

   private String desc;
   private String id;

    // getters and setters
}

Solution

  • Remove the

    binding="#{PickListConverter}"
    

    From the picklist and this error is gone

    You just don't add a converter via a binding but via a converter attribute or an f:converter converterId tag

    See also