p:PickList Spring 引导中的自定义 JSF 转换器

p:PickList Custom JSF Converter in Spring Boot

我目前正在

ClassCastException:当使用带有 p:pickList 的自定义 JSF 转换器时,com.singtel.eshop.converter.PickListConverter 无法转换为 javax.faces.component.UIComponent

我正在使用: Primefaces 4、JSF 2.2.12 和 Spring Boot 2.0.3

以下是我的代码

转换器

@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>

豆子

 @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
}

删除

binding="#{PickListConverter}"

来自选择列表,此错误已消失

您只是不通过 binding 添加转换器,而是通过 converter 属性或 f:converter converterId 标签添加转换器

另见

  • jsf custom converter