tags:

views:

656

answers:

2

Hi,

I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...

+4  A: 

You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.

Here's a basic example:

<h:selectManyCheckbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>

Bean:

private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.

public Bean() {
    // Preset the selected items.
    this.selectedItems = new ArrayList<String>();
    this.selectedItems.add("valueToBePreselected1");
    this.selectedItems.add("valueToBePreselected2");
    // Those values should be exactly the same as one of the SelectItem values.
    // I.e. the Object#equals() must return true for any of them.
}
BalusC
Thanks a lot. Actually I tried this already and it didn't work because I.....added the labels of the checkboxes, not the values ;).
Shizuma
Good to know. (+1)
Arthur Ronald F D Garcia
+1  A: 

Populate the property you use in "value" before rendereing the page (for example using a phase listener)

<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
    <f:selectItem itemLabel="India" itemValue="India" />
    <f:selectItem itemLabel="China" itemValue="China" />
    <f:selectItem itemLabel="Germany" itemValue="Germany" />
    <f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>
Bozho