2011-04-05 108 views
1

我在我的编辑用户屏幕下面的代码如何使用JSF中的复选框列表绑定数据?

<h:selectManyCheckbox id="selectedGroups" value="#{usersController.selectedGroups}"> 
       <f:selectItems value="#{usersController.groupsList}" var="item" itemLabel="#{item.groupname}" itemValue="#{item.groupid}" /> 
      </h:selectManyCheckbox> 

我已经在它所有的组用户组名单,我已经与为用户启用组selectedGroups列表。但是,在编辑屏幕上,它们并未默认显示。我错过了什么?这是不是绑定选定的许多复选框的正确方法?

回答

1

groupsList的项值将被预选只有当equals()方法在selectedGroups返回true对于至少一个项。

假设groupidLong,则selectedGroups应该返回包含值的List<Long>被预选。

0

下面的代码将很好地工作的请求范围豆...

XHML代码....

<h:selectManyCheckbox binding="#{Page1.chk_1}"> 
     <f:selectItem binding="#{Page1.chk_1_options}"/> 
</h:selectManyCheckbox> 

Java代码....

HtmlSelectManyCheckbox chk_1 = new HtmlSelectManyCheckbox(); 
UISelectItems chk_1_options = new UISelectItems(); 

public HtmlSelectManyCheckbox getChk_1() { 
    return chk_1; 
} 

public void setChk_1(HtmlSelectManyCheckbox chk_1) { 
    this.chk_1 = chk_1; 
} 

public UISelectItems getChk_1_options() { 
    if (chk_1_options.getValue() == null) { 
     List<SelectItem> lst_chk_options = new ArrayList<SelectItem>(); 
     lst_chk_options.add(new SelectItem(1, "Label1")); 
     lst_chk_options.add(new SelectItem(2, "Label2")); 
     chk_1_options.setValue(lst_chk_options); 
    } 
    return chk_1_options; 
} 

public void setChk_1_options(UISelectItems chk_1_options) { 
    this.chk_1_options = chk_1_options; 
} 

如果你想为会话范围然后回复,因为会话范围中的绑定元素在某些情况下给出问题...