getNewValue() from <h:selectManyCheckbox>
Anyone have a good approach to getting the selected items from a <h:selectManyCheckbox> item when the ActionEvent is initiated by a <h:commandButton> item?
I can't seem to get a value list from <h:selectManyCheckbox> unless it has it's own event. Walking the tree in the backing beans shows an empty value list. The facelets compositions included below have no effect on this.
My jsf with <h:selectManyCheckbox>
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets">
<h1>Currently Registered:</h1>
<h:panelGroup>
<h:selectManyCheckbox id="patients" layout="pageDirection"
value="#{patients.printList}"
><f:selectItems value="#{patients.selectList}" />
</h:selectManyCheckbox>
</h:panelGroup>
</ui:composition>
My jsf with <h:commandButton>
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets">
<h:form name="printJobForm">
<h:commandButton type="submit" value="Review Print Jobs"
id="printJobs"
action="success"
actionListener="#{printJobs.collectJobs}"
immediate="true" />
</h:form>
</ui:composition>
[1602 byte] By [
paul@sdna] at [2007-11-27 4:52:30]

# 2
Thanks for the catch, I have now corrected that and obtain the same non-results.
The backing bean method below is the simplest of many approaches I've tried to get the expected values of #{patients.selectedItems}. The value for selectItems is correct. The value for printItems should be >0, but == 0.
public void collectJobs(ActionEvent event) {
System.out.println("collectJobs called");
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
PatientController pc = (PatientController)session.get("patients");
System.out.println("selectItems=" + pc.getSelectItems().size());
System.out.println("printItems=" + pc.getPrintItems().size());
}
Message was edited by:
paul@sdn
# 3
I don't understand what you're trying to approach as your coding logic look like odd.
Generally the h:selectManyCheckbox works as follows:
JSF<h:form>
<h:selectManyCheckbox value="#{myBean.selectedItems}">
<f:selectItems value="#{myBean.selectItems}" />
</h:selectManyCheckbox>
<h:commandButton value="submit" action="#{myBean.action}" />
</h:form>
MyBeanpublic class MyBean {
private List<String> selectedItems;
private List<SelectItem> selectItems;
public void action() {
if (selectedItems != null) {
for (String selectedItem : selectedItems) {
System.out.println(selectedItem);
}
}
}
public List<SelectItem> getSelectItems() {
if (selectItems == null) {
selectItems = new ArrayList<SelectItem>();
selectItems.add(new SelectItem("value1", "label1"));
selectItems.add(new SelectItem("value2", "label2"));
selectItems.add(new SelectItem("value3", "label3"));
}
return selectItems;
}
public List<String> getSelectedItems() {
return selectedItems;
}
public void setSelectedItems(List<String> selectedItems) {
this.selectedItems = selectedItems;
}
}
Instead of String you can use any type of Object as the SelectItem value. If the Object has a human readable toString() method, which can be used as the label, then you can leave the label away from the construction of SelectItem.
# 4
Thank you for your thoughts and suggestions. Having all the functionality in one bean works fine for me as well.
But my objective is to have one bean (my PrintController) get inputs from several beans (one being my PatientContoller). Aside from some needed refactoring, this is why my posted example may seem a little odd. I'm quite reluctant to munge assorted business logic into one fat bean.
I'm presuming that my backing bean has access to the entire JSF tree and I can then track down resources from another bean. It's the selectedList resources from an entity that didn't initiate the event that don't seem to get into the JSF tree for me.
# 6
My problem turns out to be using <SelectItem> for selected values. I guess my initial learning case didn't test this aspect enough, and at first this seems naturally correct.
Thanks for your insight. You've responded to this issue in other locations, it seems this aspect could be more highlighted.