Thursday, February 17, 2011

Passing JSF action methods into ui:include

Parameters like Java plain, complex objects and beans can be transfered into an ui:include to be available in the included page. An example:
<ui:include src="/sections/edit.xhtml">
    <ui:param name="editBean" value="#{editTemplate}"/>
    <ui:param name="param1" value="value1"/>
    <ui:param name="param1" value="value1"/>
</ui:include>
The facelets edit.xhtml has then an access to these parameters. Well, but what is about action methods? They can be passed by array notation. We can include a facelets from many other facelets and specify the action by its name. E.g.
<ui:include src="/sections/edit.xhtml">
    <ui:param name="editBean" value="#{editTemplate}"/>
    <ui:param name="deleteAction" value="deleteById"/>
    <ui:param name="obj" value="#{listTemplates.selectedId}"/>
</ui:include>

<ui:include src="/sections/edit.xhtml">
    <ui:param name="editBean" value="#{editTemplate}"/>
    <ui:param name="deleteAction" value="deleteByTemplate"/>
    <ui:param name="obj" value="#{listTemplates.selectedTemplate}"/>
</ui:include>
deleteAction is an action name as String which is defined in the passed bean #{editTemplate}. In the included edit.xhtml we can access the proper action and call it with parameter as follows
 
<h:commandButton value="Delete" action="#{editBean['deleteAction'](obj)}"/> 
 
or if you don't use EL 2.x
 
<h:commandButton value="Delete" action="#{editBean['deleteAction']}">
    <f:setPropertyActionListener target="#{editBean.selectedObject}" value="#{obj}"/>
</h:commandButton> 
 
Like JavaScript objects we can use the array notation in EL expressions.

10 comments:

  1. hi
    nice article... just what i need
    but im stuck...

    seems like my included facelet cannot interpret the following EL

    ...action="#{reportRequestBean['filterAction']}"...

    the except is:

    javax.servlet.ServletException: #{reportRequestBean['filterAction']}: javax.el.MethodNotFoundException: /pages/filtri_ricerca_report.xhtml @68,125 action="#{reportRequestBean['filterAction']}": Method not found: it.enel.amr.mbeans.ReportRequestBean@4f36b91b.filterAction()



    fact is that the facelet receives and reads the params, indeed in a h:outputText it prints the value of the method to call...


    jsf 1.2
    el 1.1


    thanks bud
    ;)

    ReplyDelete
  2. You need at least EL 2.0 to be able to call methods with parameters :-) I have never tried EL 2.0 with JSF 1.2, but I think they should play nice together.

    ReplyDelete
  3. wait... i dont need any param
    just to call the method...

    its a void...

    and i read in your article

    [...]or if you don't use EL 2.x[...]


    am i wrong?!

    ReplyDelete
  4. Oh, yes, you are right. "if you don't use EL 2.x" is related to f:setPropertyActionListener. To your problem - do you sure you have the method filterAction in your bean reportRequestBean? I have such syntax in at least 2 projects and everything works fine.

    ReplyDelete
  5. yes... the method is there...

    without single-quote is working

    action="#{bean[filterAction]}"



    i am usin jsf1.2
    you?

    ReplyDelete
  6. Interesting. I'm using JSF 2 (the last Mojarra 2.1.1).

    ReplyDelete
  7. I am using jsf1.2,
    In 1.jsf i have written the code as :






    and 2.jsf i am trying to fetch value of 'contestNm' , code as ...

    #{contestNm}

    value is not coming ...
    Can anybody help regarding this.
    Thanks in Advance
    Vimala.

    ReplyDelete
  8. I am using jsf1.2,
    In 1.jsf i have written the code as :






    and 2.jsf i am trying to fetch value of 'contestNm' , code as ...

    #{contestNm}

    value is not coming ...
    Can anybody help regarding this.
    Thanks in Advance
    Vimala.

    ReplyDelete
  9. Hi,

    I am having one requirement. I need to pass the param value to the backing bean like





    I need to pass the param value to some backing bean. How can we do this? This value we will use to query the DB to display the data on the page.....

    ReplyDelete
  10. Hi,

    I am having one requirement. I need to pass the param value to the backing bean like

    ui:include src="/template/common/commonHeader.xhtml"
    ui:param name="tagLine" value="JSF value"
    /ui:param
    /ui:include

    I need to pass the param value to some backing bean. How can we do this? This value we will use to query the DB to display the data on the page

    ReplyDelete

Note: Only a member of this blog may post a comment.