Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing action method names as arguments to facelets componenets

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?

like image 862
ManiP Avatar asked Apr 14 '11 06:04

ManiP


2 Answers

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]}"
like image 63
Cristian Boariu Avatar answered Nov 12 '22 11:11

Cristian Boariu


Maybe the ActionMapperTagHandler would work.

like image 24
Sam Hasler Avatar answered Nov 12 '22 12:11

Sam Hasler