Using multiple booleans as value for selectManyCheckbox
I have a number of Booleans that I want to group together by using a h:selectManyCheckbox. I don't want the value to be an ArrayList or Array, just my XX Boolean fields. Is there anyway开发者_运维知识库 this can be accomplished?
No, you can't. Just use <h:selectBooleanCheckbox>
for each.
<h:selectBooleanCheckbox value="#{bean.boolean1}" />
<h:selectBooleanCheckbox value="#{bean.boolean2}" />
<h:selectBooleanCheckbox value="#{bean.boolean3}" />
...
I know the question is old, but in case someone (like I was) still looking for the answer
Using Java 8:
<h:selectManyCheckbox value="#{bean.checks}">
<f:selectItem itemValue="value1" itemLabel="Label 1" />
<f:selectItem itemValue="value2" itemLabel="Label 2" />
<f:selectItem itemValue="value3" itemLabel="Label 3" />
</h:selectManyCheckbox>
Bean:
private List<String> checks;
private Map<String, Runnable> checksMap;
private void initMap() {
checksMap = new HashMap<>();
checksMap.put("value1", () -> myentity.setBoolean1(true));
checksMap.put("value2", () -> myentity.setBoolean2(true));
checksMap.put("valueN", () -> myentity.setBooleanN(true));
}
When you save your entity, just iterate over the choosen checks, and set your booleans according to checksMap:
checks.stream().forEach( (e) -> checksMap.get(e).run() );
精彩评论