0

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]);
    } 
Cœur
  • 37,241
  • 25
  • 195
  • 267
CHARAFI Saad
  • 1,340
  • 3
  • 16
  • 36
  • Possible duplicate of [commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated](https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value) – Kukeltje Jul 01 '19 at 20:09

3 Answers3

1

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"/>
userDT
  • 76
  • 1
  • 2
  • 9
0

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}"
David Florez
  • 1,460
  • 2
  • 13
  • 26
-1

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"/>
nettie
  • 628
  • 1
  • 11
  • 23
  • This is a weird solution and most likely a workaround for a scope 'problem' – Kukeltje Jun 29 '19 at 07:29
  • I have researched and tested the solution, a workaround that solves the problem and allows the selectCheckboxMenu to be used until the developers fix the problem. I do not feel that you are justified in your downvote Kukeltje. – nettie Jul 01 '19 at 13:01
  • You nowhere stated it is a workaround and not a real solution. If you state that this is mearly a workaround since the real cause is unknown, I would not have downvoted. The reason I did is that I think noone should use this as a solution. And what strikes me as weird is that you state 'until the developers fix the problem'... I'd think you are the/a developer if you fix things with coding... – Kukeltje Jul 01 '19 at 13:05
  • I am a developer for my employer but not for the PF project. For PF I am a user, but I still provide value by testing their code (through using it) and pointing out issues. I could have simply used my workaround without taking the time to type it up, but I thought it would be helpful to do the latter. I have edited my question and I hope you will remove your downvote Kukeltje. – nettie Jul 01 '19 at 13:25
  • But it UNCLEAR what the problem is that you fix. There is afaik nothing in the PrimeFaces code that needs fixing in this regard (I do not know of a ticket in github, nor do you provide one). If you don't provide a real full [mcve] as an example that fails, nor provide version info (the orignal question is about 6.0 which has since been superseeded by 6.1, 6.2 and 7.0) it is hard to believe that this is a bug in PrimeFaces since it is very basic functionality. I personally still think there is a scoping issue in your code that creates a certain behaviour for which you created this workaround. – Kukeltje Jul 01 '19 at 14:02
  • Some hints: You most likely run into a combination of #3 and #4 in https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value which contain hints to check and https://www.primefaces.org/showcase/ui/input/checkboxMenu.xhtml works great... – Kukeltje Jul 01 '19 at 14:03
  • There are plenty of threads describing a problem with this component updating values, as late as 2017. Please see https://stackoverflow.com/questions/29746618/primefaces-selectcheckboxmenu-old-values-in-toggleselect-actionlistener. I described the problem in my workaround post. If you wish to contest, please provide me with a reference to proof that selectcheckboxmenu works as intended and does not clear out the backing bean array upon submit. – nettie Jul 01 '19 at 15:07
  • The proof is in the showcase... Which I linked to. And the link you refer to is for an ajax 'toggleSelect' event which is not in the question nor in your answer. So effectively a totally not related Q and A – Kukeltje Jul 01 '19 at 15:40