PrimeFaces selectOneMenu have null new value in both valueChangeListener and ajax listener

Wis picture Wis · Sep 19, 2014 · Viewed 7k times · Source

I've got a problem with PrimeFaces. Here is my XHTML code :

<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
    valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
    update=":contentform :popup">
    <f:selectItems value="#{gCnewObjectBean.branche}" />
    <p:ajax update=":contentform :popup"
        listener="#{gCnewObjectBean.brancheChangedListener}" />
</p:selectOneMenu>

And here is my bean code :

public void brancheChangedListener(ValueChangeEvent event) {
    Integer branche = (Integer) event.getNewValue();
    // do populate the second select menu based on this value
}
public void brancheChangedListener(AjaxBehaviorEvent event) {
    SelectOneMenu selectMenu = (SelectOneMenu) event.getSource();
    Integer branche = (Integer) selectMenu.getSubmittedValue();
    // do populate the second select menu based on this value
}

The HTML code generated is a bit strange with primefaces, the HTML select component is hidden. But if I see it with firebug, it looks like this :

<select id="branche_input" name="branche_input">
    <option value="0"></option>
    <option value="1">Prestations prévoyance professionnelle</option>
    <option value="2">Prestations prévoyance privée</option>
    <option value="3">Prestations PRIRENT</option>
    <option value="4">Gestion prévoyance professionnelle</option>
</select>

This code is a way to test the two solutions (valueChangeListener and the f:ajax) described by BalusC on this post.

When I do change the value in the select menu, both listener are triggered, first the valueChangeEvent, then the AjaxBehaviorEvent, which is perfectly what I suspected based on BalusC post. But both bean method do get a "null" for the new value (with debugger, I can see that the old value in the ValueChangeEvent is "0", which is correct according to my selectItems).

Any clue to solve this ? I really dont get it, I saw the code I'm using several time on this site...

Technical info : JBoss EAP 6.0, Mojarra 2.1.7-jboss, PrimeFaces 4.0

Edit : I was wondering if it was a bug in the jsf-impl library embedded in JBoss. I updated to Mojarra 2.1.18-jboss, but the problem is strictly the same : a null value is placed into my grille object by the ajax listener and a null value is also returned by the getNewValue() object of the valueChangeEvent.

Answer

Mahttias Schrebi&#233;r picture Mahttias Schrebiér · Sep 19, 2014

First of all, i think there is a problem with your f:selectItems tag. Try to set the itemValue and itemLabel attributes for it: (I think the gCnewObjectBean.branche is not a integerList,is it?)

<p:selectOneMenu value="#{gCnewObjectBean.grille.branche}"
   valueChangeListener="#{gCnewObjectBean.brancheChangedListener}"
   update=":contentform :popup">
   <f:selectItems value="#{gCnewObjectBean.brancheList}" var="item" 
       itemValue="#{item.branche}" itemLabel="#{item.label}"/>
   <p:ajax update=":contentform :popup"
    listener="#{gCnewObjectBean.brancheChangedListener}" />

Your attempt to get the selected value with the getSubmittedValue() method can't work. getSubmittedValue is not returning the value you are expecting:

See Why can I not get the submitted value from component binding?

If this is not the solution to your problem, you can try to put a p:messages tag in your jsf page. Probably there are errors which does not show up in the console.