1

I am using a PrimeFaces' pickList with a custom converter.

JSF:

<p:pickList converter="costsConverter" value="#{offerController.costsAsDualListModel}" var="cost" itemLabel="#{cost}" itemValue="#{cost}" />

offerController.costsAsDualListModel looks like this:

public DualListModel<Cost> getCostsAsDualListModel() {
    DualListModel<Cost> costsDualList;
    List<Cost> costsSource = new ArrayList<Cost>();
    List<Cost> costsTarget = new ArrayList<Cost>();
    for (Cost c : costs) {
        costsSource.add(c);
    }
    costsDualList = new DualListModel<Cost>(costsSource, costsTarget);
    return costsDualList;
}

And my custom converter looks like this:

public String getAsString(FacesContext context, UIComponent component, Object object) {
    if (object == null) {
        return "";
    }
    Integer no = ((Cost) object).getNo();
    String valueOf = String.valueOf(no);
    return valueOf;
}

getAsString() is called and valueOf is correct but inside my picklist I still see the objects and not the return value fo getAsString().

I tried to use <f:converter converterId="costsConverter"/> within the picklist element. Same issue. Also I registered the converter in faces-config.xml:

<converter>
  <converter-id>costsConverter</converter-id>
  <converter-class>com.my.converter.CostsConverter</converter-class>
</converter>

What could be the problem?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
antarkt1s
  • 135
  • 1
  • 2
  • 13
  • you have to use converterID with the `converter` attribute. this is the value of `` in the jsf app configuration file (faces-config.xml) or in the `@FacesConverter` annotation. – The Bitman May 31 '17 at 11:53
  • For the label a converter is not needed (not even used). Just use `#{cost.no} there... – Kukeltje May 31 '17 at 11:54

1 Answers1

2

You have a wrong understanding of values in components like picklists, selectonemenus, etc. These values are never displayed there but the labels are. And since converters are for values, not labels, you'll never see the converted value but the labels and everything behaves as it should. Just use itemLabel="#{cost.no}" and everything should be fine (display wise).

See e.g. how it is used in these two Q/A that also use a converter

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Thank you very much for the answer! This is the way I would use it in a `selectonemenu` but the value of a `picklist` uses a `DualListModel<>` with a source and a targer (which are both `Lists<>` elements) instead of a `List<>`. So I don't know how to access the `no`. – antarkt1s Jun 01 '17 at 06:10