如何在JSF中使用复选框列表绑定数据?
我在编辑用户屏幕中有以下代码如何在JSF中使用复选框列表绑定数据?,jsf,binding,checkbox,Jsf,Binding,Checkbox,我在编辑用户屏幕中有以下代码 <h:selectManyCheckbox id="selectedGroups" value="#{usersController.selectedGroups}"> <f:selectItems value="#{usersController.groupsList}" var="item" itemLabel="#{item.groupname}" itemValue="#{item.groupid}" /&g
<h:selectManyCheckbox id="selectedGroups" value="#{usersController.selectedGroups}">
<f:selectItems value="#{usersController.groupsList}" var="item" itemLabel="#{item.groupname}" itemValue="#{item.groupid}" />
</h:selectManyCheckbox>
我有一个包含所有组的用户组列表,我选择了一个包含为该用户启用的组的组列表。但是,在编辑屏幕上,默认情况下,它们不会显示为选中。我错过了什么?这不是绑定所选多个复选框的正确方法吗?只有当
equals()
方法为selectedGroups
中的至少一个项返回true
时,组列表的项值才会被预选
假设groupid
是一个Long
,那么selectedGroups
应该返回一个列表
,其中包含要预选的值。只有当等于()
方法已为selectedgroup
中的至少一项返回true
假设groupid
是一个Long
,那么selectedGroups
应该返回一个列表
,其中包含要预选的值。以下代码对于请求范围bean将非常有效
XHTML代码
<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;
}
HtmlSelectManyCheckbox chk_1=新的HtmlSelectManyCheckbox();
UISelectItems chk_1_options=新UISelectItems();
公共HtmlSelectManyCheckbox getChk_1(){
返回chk_1;
}
公共无效设置chk_1(HtmlSelectManyCheckbox chk_1){
this.chk_1=chk_1;
}
公共项目getChk_1_选项(){
if(chk_1_options.getValue()==null){
List lst_chk_options=new ArrayList();
lst_chk_options.add(新选择项(1,“标签1”);
lst_chk_options.add(新选择项(2,“标签2”);
chk_1_选项。设置值(lst_chk_选项);
}
返回chk_1_选项;
}
公共作废设置选项(UISelectItems设置选项){
this.chk_1_options=chk_1_options;
}
如果您想要会话作用域,那么请回答,因为会话作用域中的绑定元素在某些情况下会出现问题…以下代码将非常适合请求作用域bean
XHTML代码
<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;
}
HtmlSelectManyCheckbox chk_1=新的HtmlSelectManyCheckbox();
UISelectItems chk_1_options=新UISelectItems();
公共HtmlSelectManyCheckbox getChk_1(){
返回chk_1;
}
公共无效设置chk_1(HtmlSelectManyCheckbox chk_1){
this.chk_1=chk_1;
}
公共项目getChk_1_选项(){
if(chk_1_options.getValue()==null){
List lst_chk_options=new ArrayList();
lst_chk_options.add(新选择项(1,“标签1”);
lst_chk_options.add(新选择项(2,“标签2”);
chk_1_选项。设置值(lst_chk_选项);
}
返回chk_1_选项;
}
公共作废设置选项(UISelectItems设置选项){
this.chk_1_options=chk_1_options;
}
如果您想要会话范围,请回复,因为会话范围中的绑定元素在某些情况下会出现问题