I want to pass check boxes to a backing CDI-Bean. The postTest.values are just a List of Longs.
<h:form>
<h:dataTable value="#{postTest.values}" var="val">
<h:column>
<h:outputLabel value="#{val}"/>
</h:column>
<h:column>
<h:selectBooleanCheckbox value="#{postTest.checked[val]}"/>
</h:column>
</h:dataTable>
<h:commandButton action="#{postTest.process}"/>
</h:form>
The action method should print out the checked values. But it is just empty.
@Named
@RequestScoped
public class PostTest {
List<Long> values;
Map<Long, Boolean> checked;
...
public String process() {
logger.info(this.toString() + "Processing");
for (Long l : checked.keySet()) {
logger.info(this.toString() + "\t" + l + ". checked: " + checked.get(l));
}
return "index2";
}
...
}
When I add logging to the getChecked() method, I can see, that it is just retrieved once per column and its content isn’t changed at all.
The problem seem to be related to the point that the postTest.values is not initialized when the form passes the values. Because if I initialize the postTest.values in the constructor (or a @PostConstruct) the checked items are passed correctly.
Why do I need to initialize the postTest.values at all after the POST request is executed?
Is there a way to prevent that?
Or do I have another option? E.g. make sure that the postTest.values are initialized properly not using the constructor nor the @PostConstruct, cause I want to pass values to it before initialization (I tried listeners, but they don’t seem to solve that).
Thanks!
Tim
You can also use Myfaces CODI
@ViewAccessScopedannotation if you don’t want to play with@ConversationScopedand thestart()andend()methods. it is a CDI extension that is kind of equivalent to@ViewScopedof JSF managed bean. I did not check the implementation of this annotation but I see many developers tell that it is even better than@ViewScopedannotation.Just download Myfaces CODI and drop it in your classpath and use the annotation.