JSF/SEAM:如何预先选择表单中的复选框

Shi*_*uma 2 java jsf seam

我有一个表格,我必须预先选择一些复选框.用jsf/seam怎么可能?在简单的html中,您只需将"checked"(或checked ="checked")属性添加到复选框即可.但是对于f:selectItems我没有任何线索......对象"SelectItem"也没有为此提供任何setter ...

Bal*_*usC 6

您需要在组件属性后面的value属性中预设它们,就像通常对每个UIInput组件一样.您可以在bean的构造函数或初始化块中执行此操作.

这是一个基本的例子:

<h:selectManyCheckbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Run Code Online (Sandbox Code Playgroud)

豆:

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.
}
Run Code Online (Sandbox Code Playgroud)