0
我有一個對象,說用戶,它有10個屬性,我想綁定到h:selectManyCheckbox,並希望顯示它的兩個特定字段作爲itemLabel & f的itemValue: selectItems的。我怎樣才能做到這一點?請幫忙!如何用h綁定對象:selectManyCheckbox
我有一個對象,說用戶,它有10個屬性,我想綁定到h:selectManyCheckbox,並希望顯示它的兩個特定字段作爲itemLabel & f的itemValue: selectItems的。我怎樣才能做到這一點?請幫忙!如何用h綁定對象:selectManyCheckbox
使用您的facelet如下:
<h:selectManyCheckbox id="yourElementID"
value="#{myBean.selectedList}">
<f:converter converterId="userConverter"/>
<f:selectItems value="#{myBean.availableItemsList}"
var="item"
itemLabel="#{item.labelAttribute}"
itemValue="#{item.valueAttribute}"/>
</h:selectManyCheckbox>
替換:
myBean
與bean的名字
selectedList
與持有 選定的值
availableItemsList
與
您的項目提供
labelAttribute
與屬性的
名字,你打算爲項目
標籤使用的列表。
valueAttribute
與屬性的名稱 您打算爲項目 值使用
注意,轉換器在f:converter
元素引用。 A h:selectManyCheckbox
將字符串作爲值返回。所以你需要一個用於你的用戶類的轉換器來將對象轉換爲字符串,反之亦然。你可以將它作爲託管bean的內部類來實現,或者作爲單獨的類來實現。
@FacesConverter(value="userConverter")
public static class UserConverter implements Converter {
public Object getAsObject(FacesContext facesContext,
UIComponent component, String value) {
// your code to convert String to Object
}
public String getAsString(FacesContext facesContext,
UIComponent component, Object object) {
// your code to convert Object to String
}