You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by gi...@mac.com on 2006/05/10 16:13:01 UTC

selectManyListbox custom converter toString() problem

I'm having some trouble using a selectManyListbox with a custom  
converter.
My select items are composed of an object value and string label as  
follows:

LookupItem item = ...  // simple lookup - integer values and a  
description label.
String label = item.getLabel(locale);
SelectItem selectItem = new SelectItem(item,label);
.......
Collection<SelectItem> lookupItems = new ArrayList<SelectItem>();
.......

Selected values in the backing bean are held in an ArrayList.

<t:selectManyListbox
     value="#{mybean.selectedItems}"
     size="30" >
     <f:selectItems value="#{mybean.lookupItems }" />
     <f:converter
       converterId="com.mydomain.MyConverter" />
</t:selectManyListbox>

I've written a custom converter which works fine if I use it with a  
SelectOne.. type component but when I try it with a SelectMany..  
getAsString() is passed a String value containing the selected value  
instead of a LookupItem object.

public String getAsString(FacesContext facesContext, UIComponent  
component, Object value)
             throws ConverterException {
         if (value != null) {
             return ((LookupItem) value).getCode().toString();
         }
         return null;
}

  java.lang.ClassCastException: java.lang.String
         at com.mydomain.MyConverter.getAsString(Unknown Source)

Maybe I'm missing something but I expected getAsString() to be passed  
the converted LookupItem object.
I'd appreciate some help.
-Gianni

Re: selectManyListbox custom converter toString() problem

Posted by Mike Kienenberger <mk...@gmail.com>.
On 5/10/06, giannidoe@mac.com <gi...@mac.com> wrote:
> public String getAsString(FacesContext facesContext, UIComponent
> component, Object value)
>              throws ConverterException {
>          if (value != null) {
>              return ((LookupItem) value).getCode().toString();
>          }
>          return null;
> }

Don't know if it's relevent, but getAsString should return "" instead of null.

http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/javax/faces/convert/Converter.html