【问题标题】:How to bind data with checkbox list in JSF?如何在 JSF 中将数据与复选框列表绑定?
【发布时间】:2011-04-05 08:28:42
【问题描述】:

我的编辑用户屏幕中有以下代码

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

我有一个包含所有组的用户组列表,并且我选择了包含为用户启用的组的组列表。但是,在编辑屏幕上,默认情况下它们没有显示为选中状态。我错过了什么?这不是绑定选中的许多复选框的正确方法吗?

【问题讨论】:

    标签: jsf binding checkbox


    【解决方案1】:

    仅当equals() 方法为selectedGroups 中的至少一项返回true 时,才会预选groupsList 的项值。

    假设groupidLong,那么selectedGroups 应该返回一个包含要预选的值的List&lt;Long&gt;

    【讨论】:

      【解决方案2】:

      以下代码将完美地适用于请求范围 bean...

      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;
      }
      

      如果您想要会话范围,请回复,因为会话范围内的绑定元素在某些情况下会出现问题...

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 2019-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多