Passing parameters to a4j:ajax method

user969743 picture user969743 · Sep 28, 2011 · Viewed 9.1k times · Source

I am trying to use <a4j:ajax> to feed a method with a value just entered on the form;

<h:selectOneMenu id="aa" value="#{colorClass.color}">
    <f:selectItems value="#{myChoices.colorOptions}"/>
    <a4j:ajax event="change" render="colorCode" 
        execute="#{myChoices.getColorCode(colorClass,colorClass.color)}"/>
</selectOneMenu>

Color on the form is selected correctly;

my problem is that when I pass colorClass.color as part of the execute, it is blank; if I replace colorClass.color with a literal

<a4j:ajax event="change" render="colorCode" 
    execute="#{myChoices.getColorCode(colorClass,'green')}"/>

the method is called, finds the colorCode and repaints the form

How can I "grab" the value just entered so that I can pass it as a parameter to the method?

Answer

BalusC picture BalusC · Sep 28, 2011

You need listener attribute instead of execute attribute. The execute attribute should point to a collection of client IDs which are to be submitted (which defaults to @this in <f:ajax> and @form in <a4j:ajax>). However in your particular case it returns void and keeps the execute empty. The listener attribute is supposed to point to a bean action listener method. Fix it accordingly:

<a4j:ajax event="change" render="colorCode" 
    listener="#{myChoices.getColorCode(colorClass,colorClass.color)}"/>

Note that the colorClass argument seems superfluous here, or at least the colorClass.color as you could also just do colorClass.getColor() inside the getColorCode() method. Just passing one of them ought to be sufficient. Passing colorClass.color would be preferable so that your myChoices bean is not tight coupled with the colorCode bean.

<a4j:ajax event="change" render="colorCode" 
    listener="#{myChoices.getColorCode(colorClass.color)}"/>