You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Thomas Hamacher <ha...@project-team.de> on 2009/10/08 14:52:02 UTC

Converter issue

Hi @all,

I´m struggling around with the converters and maybe someone can give me,
what I´m doing wrong.

I use the following code in my web-page:

<tr:selectOneChoice id="country"
    value="#{signUpForm.country}"
    simple="true"
    required="true"
    converter="#{commonDataHelper.countryConverter}"
    unselectedLabel="#{messages['label.address.country.unselected']}">
        <f:selectItems value="#{commonDataHelper.countrySelectionList}" />
</tr:selectOneChoice>

The selectItems are filled by the following method:

public Map<String, Country> getCountrySelectionList() {

    Map<String, Country> sortedCountryList = new TreeMap<String,
Country>();

    for (Country country : getCountryManager().getAllCountries()) {
        String label = CountryConverter.getMessage(country);
        sortedCountryList.put(label, country);
    }

    return sortedCountryList;
}

Now as soon as my converter´s getAsObject()-method is called, I get the
toString() representation of my country´s object as the String-parameter.
But this is not what I need, as this is of no value to me. What would be
more interesting would be the selected label, from which I can convert back
to my entity. If I return a Map<String, String> the label is given
correctly, but then - after returning the correct Country-Object - it
obviously cannot find this object in it´s selectionList and returns an
error.

Is there anything I have to keep in mind or do I absolutely misunderstand
the converters? For my understanding the getAsString()-method should
convert an object into a String, which is used in the select-box and the
getAsObject()-methods reconverts this String back to an object. I don´t
think it´s meant to be, that the conversion is relying on the
representation of the toString()-method and that this should be used for
comparison. This would be a very unreliable option.

Maybe someone can help me to understand this whole thing.

Thanks
Thomas

Re: Converter issue

Posted by Anton Gavazuk <an...@gmail.com>.
Thomas,

in your case

getAsString returns string representation of object and the representation
can be checked in HTML source  - see "value" attributes of HTML select
element
getAsObject method gets String value from the "value" attribute of HTML
element

so in order to see "label" as parameter to getAsObject, you need to do:
Map<String, String> sortedCountryList = new TreeMap<String,String>();
sortedCountryList.put(label, label);

In order to work with select elements, I would recommend to use
javax.faces.model.SelectItem class
instead.

2009/10/8 Thomas Hamacher <ha...@project-team.de>

> Hi @all,
>
> I´m struggling around with the converters and maybe someone can give me,
> what I´m doing wrong.
>
> I use the following code in my web-page:
>
> <tr:selectOneChoice id="country"
>    value="#{signUpForm.country}"
>    simple="true"
>    required="true"
>    converter="#{commonDataHelper.countryConverter}"
>    unselectedLabel="#{messages['label.address.country.unselected']}">
>        <f:selectItems value="#{commonDataHelper.countrySelectionList}" />
> </tr:selectOneChoice>
>
> The selectItems are filled by the following method:
>
> public Map<String, Country> getCountrySelectionList() {
>
>    Map<String, Country> sortedCountryList = new TreeMap<String,
> Country>();
>
>    for (Country country : getCountryManager().getAllCountries()) {
>        String label = CountryConverter.getMessage(country);
>        sortedCountryList.put(label, country);
>    }
>
>    return sortedCountryList;
> }
>
> Now as soon as my converter´s getAsObject()-method is called, I get the
> toString() representation of my country´s object as the String-parameter.
> But this is not what I need, as this is of no value to me. What would be
> more interesting would be the selected label, from which I can convert back
> to my entity. If I return a Map<String, String> the label is given
> correctly, but then - after returning the correct Country-Object - it
> obviously cannot find this object in it´s selectionList and returns an
> error.
>
> Is there anything I have to keep in mind or do I absolutely misunderstand
> the converters? For my understanding the getAsString()-method should
> convert an object into a String, which is used in the select-box and the
> getAsObject()-methods reconverts this String back to an object. I don´t
> think it´s meant to be, that the conversion is relying on the
> representation of the toString()-method and that this should be used for
> comparison. This would be a very unreliable option.
>
> Maybe someone can help me to understand this whole thing.
>
> Thanks
> Thomas
>