[英]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>
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.