简体   繁体   English

JSF:如何绑定许多h:selectBooleanCheckbox?

[英]JSF: How to bind many of h:selectBooleanCheckbox?

I have a problem to bind list of h:selectBooleanCheckbox to my bean. 我有一个问题是将h:selectBooleanCheckbox的列表绑定到我的bean。 Anybody helps ? 有人帮忙吗?

This is not working: 这不起作用:

<ui:repeat value="#{cartBean.productsList}" var="cartProduct" varStatus="i">
   <h:selectBooleanCheckbox binding="#{cartBean.checkboxes[i.index]}" />
</ui:repeat>

public class CartBean extends BaseBean {
  public List<Product> getProductsList() {...}

  private HtmlSelectBooleanCheckbox[] checkboxes;
  public HtmlSelectBooleanCheckbox[] getCheckboxes() {
    return checkboxes;
  }
  public void setCheckboxes(HtmlSelectBooleanCheckbox[] checkboxes) {
    this.checkboxes = checkboxes;
  }
}

I get error: 我收到错误:

javax.faces.FacesException: javax.el.PropertyNotFoundException: /WEB-INF/flows/main/cart.xhtml @26,97 binding="#{cartBean.checkboxes[i.index]}": Target Unreachable, 'checkboxes' returned null

I solved my problem. 我解决了我的问题。 I used code like below and get what i want (thanks to BalusC blog - http://balusc.blogspot.com/2006/06/using-datatables.html#SelectMultipleRows ): 我使用下面的代码得到我想要的东西(感谢BalusC博客 - http://balusc.blogspot.com/2006/06/using-datatables.html#SelectMultipleRows ):

<ui:repeat value="#{cartBean.productsList}" var="cartProduct" varStatus="i">
  <h:selectBooleanCheckbox value="#{cartBean.selectedIds[cartProduct.id]}" />
</ui:repeat>

public class CartBean extends BaseBean {
  private Map<Integer, Boolean> selectedIds = new HashMap<Integer, Boolean>();
  public Map<Integer, Boolean> getSelectedIds() {
    return selectedIds;
  }
}

Your concrete problem is caused because the binding attribute is evaluated during view build time, that moment when the XHTML source code is turned into a JSF UI component tree, while the <ui:repeat> runs during view render time, that moment when the JSF UI component tree needs to produce HTML. 您的具体问题是由于binding属性是在视图构建期间评估的,即XHTML源代码转换为JSF UI组件树的那一刻,而<ui:repeat>在视图渲染时运行,JSF的那一刻UI组件树需要生成HTML。

In other words, the #{i.index} is only available during view render time and evaluates as null during view build time. 换句话说, #{i.index}仅在视图渲染时可用,并在视图构建时评估为null In effects, you're doing a binding="#{cartBean.checkboxes[null]}" 在效果中,你正在做一个binding="#{cartBean.checkboxes[null]}"

There's another conceptual mistake here: you seem to expect that the <ui:repeat> produces physically multiple <h:selectBooleanCheckbox> components. 这里还有另一个概念错误:您似乎期望<ui:repeat>生成多个<h:selectBooleanCheckbox>组件。 This is untrue. 这是不真实的。 There's physically only one <h:selectBooleanCheckbox> which is reused multiple times to produce HTML based on the currently iterated variable. 实际上只有一个 <h:selectBooleanCheckbox>可以多次重复使用,以根据当前迭代的变量生成HTML。 Actually, binding="#{cartBean.checkbox}" was been sufficient. 实际上, binding="#{cartBean.checkbox}"已经足够了。 However, collecting the values is a story apart. 然而,收集价值观是一个独特的故事。 I won't go in detail, but you can find several hints in this answer: Validate order of items inside ui:repeat . 我不会详细介绍,但你可以在这个答案中找到几个提示: 验证ui中的项目顺序:重复

In order to achieve the (apparent) concrete functional requirement of generating physically multiple <h:selectBooleanCheckbox> components and binding each to a separate array item, you should be using an iteration component which runs during view build time instead of view render time. 为了实现生成物理上多个<h:selectBooleanCheckbox>组件并将每个组件绑定到单独的数组项的(明显的)具体功能要求,您应该使用在视图构建时间而不是视图渲染时间运行的迭代组件。 That's the JSTL <c:forEach> : 这是JSTL <c:forEach>

<c:forEach items="#{cartBean.productsList}" var="cartProduct" varStatus="i">
    <h:selectBooleanCheckbox binding="#{cartBean.checkboxes[i.index]}" />
</c:forEach>

But, after all, using binding on a bean property should be avoided as much as possible. 但是,毕竟应尽可能避免在bean属性上使用binding Use instead exactly that attribute which you ultimately need: the value attribute. 请准确使用您最终需要的属性: value属性。 This way you don't need to do a HtmlSelectBooleanCheckbox#getValue() everytime. 这样您就不需要每次都执行HtmlSelectBooleanCheckbox#getValue() You already figured the right solution with a Map<Integer, Boolean> selectedIds : 您已经使用Map<Integer, Boolean> selectedIds了正确的解决方案:

<ui:repeat value="#{cartBean.productsList}" var="cartProduct">
    <h:selectBooleanCheckbox value="#{cartBean.selectedIds[cartProduct.id]}" />
</ui:repeat>

See also: 也可以看看:

I don't know if you can bind elements stored in an array. 我不知道你是否可以绑定存储在数组中的元素。 But in your code, the problem is that your HtmlSelectBooleanCheckbox[] is null . 但在您的代码中,问题是您的HtmlSelectBooleanCheckbox[]null So maybe change your Java code to: 所以可能将您的Java代码更改为:

public HtmlSelectBooleanCheckbox[] getCheckboxes() {
    if (checkboxes == null) {
        checkboxes = new HtmlSelectBooleanCheckbox[getProductsList().size()];
    }
    return checkboxes;
}

but I am really not sure if it will work... Maybe the solution is to not bind your HtmlSelectBooleanCheckbox elements in the Java code. 但我真的不确定它是否会起作用......也许解决方法是不在Java代码中绑定您的HtmlSelectBooleanCheckbox元素。 Why do you need to bind them? 你为什么需要绑定它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用 <h:selectBooleanCheckbox> 按下 <a4j:commandButton> 在jsf中 - How disable a <h:selectBooleanCheckbox> pressing a <a4j:commandButton> in jsf JSF 1.1-如何在备用Bean中获取h:selectBooleanCheckbox的ID属性 - JSF 1.1 - How to get the ID attribute of h:selectBooleanCheckbox in backing bean 未触发的事件 <h:selectBooleanCheckbox> 在JSF 2中 - Events not fired from <h:selectBooleanCheckbox> in JSF 2 如何使用JSF的h:selectBooleanCheckbox和h:dataTable来获取选中的复选框而不提交页面内容? - How to use JSF's h:selectBooleanCheckbox with h:dataTable to get selected checkboxes without submitting the page content? 如何设置ah:selectBooleanCheckbox的边框样式? - How to style the border of a h:selectBooleanCheckbox? jsf中的SelectBooleanCheckbox - SelectBooleanCheckbox in jsf 使用h:selectBooleanCheckbox和c:if来获取a的行值 <rich:datatable> -JSF - Using h:selectBooleanCheckbox with c:if to obtain row value of a <rich:datatable> - JSF 是否有JSF切换组件或以h:selectBooleanCheckbox为主题设置图像的方法? - Is there a JSF toggle component or a way to theme an h:selectBooleanCheckbox with images? 使用h:selectBooleanCheckbox删除JSF数据表中的多行 - Using h:selectBooleanCheckbox to delete multiple rows in JSF datatable 如何通过使用JavaScript获得JSF selectBooleanCheckbox的值? - How to get a value of JSF selectBooleanCheckbox by using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM