How to pass the selected values from selectcheckBoxMenu to my bean?

3.8k Views Asked by At

I'm using JSF 2.2.8 and primefaces 6.0, and i have a selectCheckBoxMenu i want to retrieve the selected values in my bean.

The selectCheckboxMenu is filled from the database but when i select the attributes and I save nothing happens it does not call the save function

Here is my selectCheckBoxMenu

                          <p:outputLabel for="ressource" value="Ressource"/>
                        <h:panelGroup >         
                         <p:selectCheckboxMenu id="ressource" label="Ressource" value="#{affectationBean.selectedRessource}"  multiple="true">
                                <f:selectItems value="#{affectationBean.ressources}" var="r" itemLabel="#{r.nom}" itemValue="r.idt_ressource" />
                            </p:selectCheckboxMenu>
                         </h:panelGroup>
                        <p:commandButton icon="ui-icon-save"  actionListener="#{affectationBean.save}" value="Save" update="@affectation" ajax="false" style="display:inline-block;margin-top:5px"/>

Here is the the declaration of the selectedRessource and the actionListener save

    private Long [] selectedRessource;
    // Getters setters and Construct


    public void save(){

    for(int i=0 ;i<selectedRessource.length;i++){
    system.out.println("id ===> " + selectedRessource[i]);
    } 
3

There are 3 best solutions below

0
David Florez On

The problem is in your p:commandButton, you have 3 options

change your method:

public void save(ActionEvent actionEvent){...}

change your action listener value:

actionListener="#{affectationBean.save()}"

or change your button to use action

action="#{affectationBean.save}"
0
userDT On

My suggestion would be: First make sure everything is inside the h:form tag. don't need to multiple = true as this tag does not take this attribute i tested with below modification and got the selected multiple value in my bean. The only difference is i am using same value for itemLabel and itemValue but in your case it is object. i am using primefaces 6 also and dont even need to change actionListner to action. It is working as it is.sample xhtml sample ResourceBean.java

                                  <p:outputLabel for="ressource" value="Ressource"/>
                    <h:panelGroup >         
                     <p:selectCheckboxMenu id="ressource" label="Ressource" value="#{resourceBean.selectedRessource}">
                            <f:selectItems value="#{resourceBean.ressources}" var="r" itemLabel="#{r}" itemValue="#{r}" />
                        </p:selectCheckboxMenu>
                     </h:panelGroup>
                    <p:commandButton icon="ui-icon-save"  actionListener="#{resourceBean.save}" value="Save" ajax="false" style="display:inline-block;margin-top:5px"/>
8
nettie On

DISCLAIMER: This is a workaround. It is not intended to be a permanent solution but will allow you to use selectCheckboxMenu and keep working.

There is an issue with this component that prevents it from passing values to the backing bean upon submit.

For some reason the array that should contain the selected values gets cleared out upon submit. Therefore I made an extra array that I did not declare in the tag, and updated in on every change event. Then on submit the values were still there. See below:

BackingBean.java

private String[] sCodes;
private String[] sCodes2; //extra array, not in form.xhtml

public void updateCodes()
    {       
        sCodes2 = sCodes; //keeps the values in the other array
    }

public void doSend() throws IOException
    {
log.trace("selected codes: {} selected codes2 length: {}", sCodes.length, sCodes2.length);
    }

form.xhtml

<p:selectCheckboxMenu id="codeCtl" value="#{bean.SCodes}" label="Codes" filter="true" filterMatchMode="startsWith" panelStyle="width:250px">
<f:selectItems value="#{bean.menuCodes}" />
<p:ajax event="change" listener="#{bean.updateCodes()}" />                                        
</p:selectCheckboxMenu>
<p:commandButton value="submit" actionListener="#{bean.doSend}" id="ctlSubmit" update="appform"/>