1 Reply Latest reply on Oct 27, 2009 6:38 AM by nbelaevski

    How to list selectItems in Converter

    akaine

      Hello. Currently I am fighting with comboBox trying to make label texts appear instead of id numbers. I know comboBox is fine and it's just a bunch of inputs with only one input/output. Still I was thinking to implement converter class to temporally transform the outputs.

      So let's imagine I have this in my template:

      <h:outputText value="State:" />
      <rich:comboBox value="#{user.idState}" id="idState" defaultLabel="put some value" required="true" converter="catalogueConverter" >
       <f:selectItems value="#{catalogue.states}" />
      </rich:comboBox>


      Id it possible and how could I access and list the selectItems values and labels considering that I filled them implicitly through SelectItem constructor providing with both.

      public class CatalogueConverter implements Converter{
      
       public CatalogueConverter(){}
      
       @SuppressWarnings("unchecked")
       public Object getAsObject(FacesContext fContext, UIComponent uic, String param){
       List<SelectItem> selectItems = (List<SelectItem>)uic.getAttributes().get("selectItems");
       Iterator<SelectItem> iterator = selectItems.iterator();
       while(iterator.hasNext()){
       SelectItem item = iterator.next();
       System.out.println("label="+item.getLabel()+", value="+item.getValue());
       }
       return (Object)param;
       }
      
       @SuppressWarnings("unchecked")
       public String getAsString(FacesContext fContext, UIComponent uic, Object obj){
       List<SelectItem> selectItems = (List<SelectItem>)uic.getAttributes().get("selectItems");
       Iterator<SelectItem> iterator = selectItems.iterator();
       while(iterator.hasNext()){
       SelectItem item = iterator.next();
       System.out.println("label="+item.getLabel()+", value="+item.getValue());
       }
       return "sometext";
       }
      }


      In the last case in setAsString() selectItems is null so I get NullPointer exception in Iterator iterator = selectItems.iterator();. My conclusion is that I'm filling selectItems in a wrong way, maybe by wrong walking through the elements.

      Any ideas? Or is there any other more or less automated solution to my problem?

        • 1. Re: How to list selectItems in Converter
          nbelaevski

          Hi,

          Walk through children of "uic" UIComponent and cast them to javax.faces.component.UISelectItems or javax.faces.component.UISelectItem, then use getValue(). Also you can define combo box items as plain collection without using SelectItem - check documentation.