Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

java - Primefaces Picklist Converter

I am using Primefaces 4.0 and JSF 2.2. I used a picklist with a converter. The problem is that I don't get the correct arg2 in my converter. It always says 0. My expectation is that this is the id of the element and I could parse it out of source/target lists. Any Ideas?

My Converter is inspired by How to write a custom converter for <p:pickList>.

My Picklist declaration is as follows:

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For picklist use this 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;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...