18

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?

ManiP
  • 683
  • 1
  • 7
  • 19

2 Answers2

53

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]}"
Cristian Boariu
  • 9,331
  • 12
  • 87
  • 159
  • 7
    Bless you sir! You saved me putting my fist through the monitor, or worse, having to redesign my application. – Greg Charles Jan 06 '12 at 23:28
  • If there wasn't a method named "productEditAction" in the controller, it will give an error at runtime instead of compile time because we are passing method name as a string instead of passing method directly (kinda like javascript) right? – uylmz Apr 01 '14 at 12:07
  • Wait, where does this itemEditAction parameter assignation take place? Neither ui:param nor ui:include have such parameters. – El Suscriptor Justiciero Oct 20 '20 at 08:43
2

Maybe the ActionMapperTagHandler would work.

Sam Hasler
  • 13,310
  • 9
  • 68
  • 101