Primefaces 选项列表转换器 [英] Primefaces Picklist Converter

查看:15
本文介绍了Primefaces 选项列表转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的 Converter 的灵感来自 如何为 编写自定义转换器.

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>

推荐答案

对于选择列表,使用这个 generic converter :

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

}

这篇关于Primefaces 选项列表转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆