【问题标题】:Primefaces Picklist ConverterPrimefaces 选项列表转换器
【发布时间】:2014-06-06 08:34:20
【问题描述】:

我正在使用 Primefaces 4.0 和 JSF 2.2。我使用带有转换器的选项列表。问题是我的转换器中没有正确的arg2。它总是说0。我的期望是这是元素的 id,我可以将它从源/目标列表中解析出来。有什么想法吗?

我的Converter 的灵感来自How to write a custom converter for <p:pickList>

我的 Picklist 声明如下:

<p:pickList value="#{loadingPlaceGroups.pickList}"
    style="margin:0" var="loadingPlace"
    converter="primeFacesPickListConverter"
    itemValue="#{loadingPlace}"
    itemLabel="#{loadingPlace.name}#{loadingPlace.location.address.street}#{loadingPlace.location.address.houseNr}#{loadingPlace.location.address.zipCode}#{loadingPlace.location.address.city}"
    showSourceFilter="true" showTargetFilter="true"
    filterMatchMode="contains"
    styleClass="picklist500x350source picklist500x350target">

    <f:facet name="sourceCaption">Alle Ladestellen</f:facet>
    <f:facet name="targetCaption">Gewählte Ladestellen</f:facet>
    <p:column style="border-bottom:1px solid lightgray">
        <p:panelGrid>
            <p:row>
                <p:column style="padding-left:0;font-size:12pt">
                    <h:outputLabel value="#{loadingPlace.name}"
                        style="font-weight:bold" />
                </p:column>
            </p:row>
            <p:row>
                <p:column style="padding:0">
                    <h:outputLabel
                        value="#{loadingPlace.location.address.street} #{loadingPlace.location.address.houseNr}" />
                </p:column>
            </p:row>
            <p:row>
                <p:column style="padding:0">
                    <h:outputLabel
                        value="#{loadingPlace.location.address.zipCode} #{loadingPlace.location.address.city}" />
                </p:column>
            </p:row>
        </p:panelGrid>
    </p:column>
</p:pickList>

【问题讨论】:

    标签: java primefaces converter picklist


    【解决方案1】:

    对于选择列表,请使用此generic converter

    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.UUID;
    import java.util.WeakHashMap;
    
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.faces.convert.FacesConverter;
    
    @FacesConverter(value = "entityConverter")
    public class EntityConverter implements Converter {
    
        private static Map<Object, String> entities = new WeakHashMap<Object, String>();
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object entity) {
            synchronized (entities) {
                if (!entities.containsKey(entity)) {
                    String uuid = UUID.randomUUID().toString();
                    entities.put(entity, uuid);
                    return uuid;
                } else {
                    return entities.get(entity);
                }
            }
        }
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
            for (Entry<Object, String> entry : entities.entrySet()) {
                if (entry.getValue().equals(uuid)) {
                    return entry.getKey();
                }
            }
            return null;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2012-05-26
      • 2021-03-26
      • 2013-01-10
      相关资源
      最近更新 更多