Adverts

This little Java Server Faces problem is really annoying and makes selectMany tags a pain to use. For some reason selectMany doesn't allow you to update the selected values if the selected values are stored in a list. I can only think that for some reason the JSF framework is unable to create a List. The code below won't work but as always a working version is shown at the bottom.

The Busted Code

Here is the code that you would think would work. There is a simple List of Gender objects which are the selected objects from the List of all genders (yes I know there are only two - it's an example) stored in a Genders object (as SelectItem instances). The converter works correctly so I have not shown that here.

public class Search {
   
private List<Gender> genders;
   
public List<Gender> getGenders() {
       
return genders;
   
}
   
public void setGenders(List<Gender> genders) {
       
this.genders = genders;
   
}
}

A fragment from the JSF page:

<h:panelGroup>
    <h:selectManyCheckbox id=
"gender" layout="textDirection"
   
value="#{advanced_search.genders}"
   
converter="com.crazysquirrel.GenderKeyConverter"
   
border="0">
        <f:selectItems value=
"#{genders.genderItems}"/>
    </h:selectManyCheckbox>
    <h:message
for="gender" styleClass="error"/>
</h:panelGroup>

When the page is displayed the genders are correctly ticked to match those that are selected. The problem is when the selection is change and the model updated. Rather than create a list of Gender objects and then call setGenders as "Validation Error: Value is not valid." message is displayed.

The Solution

The solution to this one seems a bit silly (to me at least). Rather than use a List use an array of the specified type. For some reason it just magically works then. The functioning code is shown below.

public class Search {

   
private Gender[] genders;

   
public Gender[] getGenders() {
       
return genders;
   
}

   
public void setGenders(Gender[] genders) {
       
this.genders = genders;
   
}
}

Adverts

Donate and Help

Please support this site and
Bandwidth doesn't grow on trees y' know :o)

Adverts

Get Adsense