Search code examples
jsf-2richfacesjsf-1.2

Passing parameter to a4j:ajax


We are upgrading from jsf 1.2 to jsf 2. We are using apache myfaces 2.1 and rich faces 4.3.

Below is the xhtml code :

Prior to Migration :

<h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
                        <a4j:support event="onclick" ajaxSingle="true" actionListener="#{bean.processInput}" reRender="compId">
                                <f:param name="name" value="paramValue"/>
                            </a4j:support>  
</h:selectBooleanCheckbox>

We are passing one parameter with and accept the parameter in actionListener method as : (String)context.getExternalContext().getRequestParameterMap().get("name1");

After Migration :

<h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
                <a4j:ajax event="click" listener="#{bean.processInput}" execute="@this" render="compId"/>                  
</h:selectBooleanCheckbox>

I want to pass a parameter to bean.processInput method which has following signature :

public void processInput(AjaxBehaviorEvent event){

According to this post - Issues in passing parameter in f:ajax , we cannot use <f:param> (its not working also) and we are not using EL 2.2 which rules out passing parameter in method signature.

Since we cannot use context.getApplication().evaluateExpressionGet due to constraints in our xhtml page only option available is use <a4j:param name="" value="" assignTo=""/>. But this needs to have one variable defined in bean requiring code change.

So my question is can we pass a parameter from UI to listener in second case and without changing the code.


Solution

  • You can use f:attribute like this :

    <h:selectBooleanCheckbox id="comp1" value="#{bean.select}">
        <a4j:ajax event="click" listener="#{bean.processInput}" execute="@this" render="compId"/>              
        <f:attribute name="name" value="paramValue" /> 
    </h:selectBooleanCheckbox>
    

    And use it in your bean :

    public void processInput(AjaxBehaviorEvent event)
    {
        System.out.println(event.getComponent().getAttributes().get("name"));
    }