Pages

Friday, September 23, 2011

Pass Parameter to ActionListener Method

This post illustrates way to send additional parameters to actionListener method.

Any actionListener method accept only one parameter actionEvent. So to pass additional parameter to actionListener method add attribute tag from JSF core. Also set the parameter value to the value that needs to passed to actionListner method.

<af:commandButton text="TestButton" id="cb1"
                        actionListener="#{pageFlowScope.TestTaskflowBean.testButtonActionListener}">
        <f:attribute name="testParam" value="#{'123'}"/>
      </af:commandButton>


Next part is how to retrieve the value ? In order to retrieve the value use the following code :

String param =
        (String)actionEvent.getComponent().getAttributes().get("testParam")
;


Sample application can be downloaded from ParamToActionListener