Passing action method names as arguments to facelets componenets

ManiP picture ManiP · Apr 14, 2011 · Viewed 9.9k times · Source

I am calling a template and am passing in parameters like below:

<ui:include src="WEB-INF/Subviews/ProductEdit.xhtml">
    <ui:param name="items" value="#{produtList}"></ui:param>
    <ui:param name="itemToEdit" value="#{productToEdit}"></ui:param>
</ui:include>

and in the ProductEdit.xhtml, I have something like

<ui:repeat value="#{items}" var="item">
  <tr>
    ...
    ...
    <td style="text-align: center">
      <h:commandLink style="cssGenericColumn" action="#{productEditAction}">
         <f:setPropertyActionListener target="#{itemToEdit}" value="#{item}"/>
      </h:commandLink>    
    </td>
  <tr>
</ui:repeat>

which works fine.

I now want to parameterize the #{productEditAction} in the ProductEdit.xhtml and so I did the following

<ui:include src="WEB-INF/Subviews/ProductEdit.xhtml">
    <ui:param name="items" value="#{produtList}"></ui:param>
    <ui:param name="itemToEdit" value="#{productToEdit}"></ui:param>
    <ui:param name="itemEditAction" value="#{productEditAction}"></ui:param>
</ui:include>

in the first page and then in ProductEdit.xhtml I do

<ui:repeat value="#{items}" var="item">
  <tr>
    ...
    ...
    <td style="text-align: center">
      <h:commandLink style="cssGenericColumn" action="#{itemEditAction}">
         <f:setPropertyActionListener target="#{itemToEdit}" value="#{item}"/>
      </h:commandLink>    
    </td>
  <tr>
</ui:repeat>

and this fails on the following error

javax.faces.el.EvaluationException: /WEB-INF/Subviews/ProductEdit.xhtml @45,89 action="#{itemEditAction}": Identity 'itemEditAction' does not reference a MethodExpression instance, returned type: java.lang.String
at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:    at javax.faces.component.UICommand.broadcast(UICommand.java:109)....
 ....
 ....
 ....

This however works if the action bound to the model object. So something like

 <h:commandLink style="cssGenericColumn" action="#{item.editAction}">

Any ideas?

Answer

Cristian Boariu picture Cristian Boariu · Apr 14, 2011

Passing a method as a parameter should be done in this way:

itemBean="#{bean}"
itemEditAction="productEditAction"

and in your component you will put them togheter:

action="#{itemBean[itemEditAction]}"