2009-11-24 105 views
2

我有一個表單,我必須預先選擇一些複選框。 jsf/seam怎麼可能?在純html中,您只需將「checked」(或checked =「checked」)添加爲複選框的屬性即可。但與f:selectItems我沒有線索...對象「SelectItem」不提供任何設置這...JSF/SEAM:如何預先選擇表單中的複選框

回答

6

您需要在組件的value屬性後面的屬性中預設它們,就像您通常一樣每個UIInput組件。你可以在bean的構造函數或初始化塊中做到這一點。

這裏有一個基本的例子:

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

豆:

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. 
} 
+0

非常感謝。其實我已經嘗試過了,它不起作用,因爲我.....添加了複選框的標籤,而不是值;)。 – Shizuma

+0

很高興知道。 (1) –

1

填充你(使用階段監聽器爲例)

<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> 
rendereing頁面之前在 「值」 使用屬性
相關問題