Hi all,

Something a little weird happens when using the selectMany components with any other object apart from Strings. I tried creating a selectManyListbox which contains some Long objects. What is tricky is the type of object I use for the selected items. If I use a List of Longs I get a validation error (I use h:messages to find out what is the problem), whereas if I use a Long[] everything works just fine.
The code that works is shown below:

My jsf view snippet is:
           <h:form>
                 <h:outputText value="Longs' Example"/>
<h:selectManyListbox id="problem" layout="pageDirection" value="#{testComponentsAction.selectedLongs}"> <f:selectItems value="#{testComponentsAction.longs}" />
                 </h:selectManyListbox>
                 <h:message for="problem" />
<h:commandButton action="#{testComponentsAction.doSearch}" actionListener="#{testComponentsAction.handleSearch}" value="Search" />
              </h:form>

An the backing bean implementation contains:

   private Long selected;
   private List<SelectItem> longs = new ArrayList<SelectItem>();
   private Long[] selectedLongs;

   public Long getSelected(){
       return this.selected;
   }
   public void setSelected(Long selected){
       this.selected = selected;
   }
public List<SelectItem> getLongs(){
       return this.longs;
   }
   public void setLongs(List<SelectItem> longs){
       this.longs = longs;
   }
public Long[] getSelectedLongs(){
       return this.selectedLongs;
   }
   public void setSelectedLongs(Long[] selectedLongs){
       this.selectedLongs = selectedLongs;
   }
With the method below I populate the selectManyListbox at the time of the rendering of the page:

public void loadLongs(){
       try {
           System.out.println("loadLongs()");
longs.add(new SelectItem(new Long(18), "18"));
           longs.add(new SelectItem(new Long(19), "19"));
           longs.add(new SelectItem(new Long(20), "20"));
selectedLongs = new Long[3];
           selectedLongs[0] = new Long(18);
           selectedLongs[1] = new Long(19);
           selectedLongs[2] = new Long(20);
} catch (RuntimeException re) {
           System.out.println("get failed");
           throw re;
       }
   }



If I use a List / ArrayList ... for the selectedLongs variable which holds the set of the selected items I get a validation error. Does anyone know why this happens. It took me a lot of time to figure out what was causing the problem and try out the use of a table.

Thanks,
Elenh

Reply via email to