4

I have a page which includes content from another page dynamically (this is done by a method in the bean)

firstPage.xhtml

<ui:include src="#{managedBean.pageView}">
    <ui:param name="method" value="#{managedBean.someAction}"/>
</ui:include>

This redirects to a secondPage which is within <ui:composition> which has commandButton.

secondPage.xhtml

<ui:composition>
..
..
<p:commandButton actionListener=#{method} value="Submit"/>
</ui:composition>

ManagedBean

public String pageView(){
return "secondPage.xhtml";
}

public void someAction(){
*someAction*
}

The commandButton in the secondPage.xhtml is not working.

Any help shall be much appreciated.

sciFi
  • 161
  • 1
  • 2
  • 9

1 Answers1

10

You can't pass method expressions via <ui:param>. They're interpreted as value expression.

You've basically 3 options:

  1. Split the bean instance and the method name over 2 parameters:

    <ui:param name="bean" value="#{managedBean}" />
    <ui:param name="method" value="someAction" />
    

    And couple them in the tag file using brace notation [] as follows:

    <p:commandButton action="#{bean[method]}" value="Submit" />
    

  2. Create a tag handler which converts a value expression to a method expression. The JSF utility library OmniFaces has a <o:methodParam> which does that. Use it as follows in the tag file:

    <o:methodParam name="action" value="#{method}" />
    <p:commandButton action="#{action}" value="Submit" />
    

  3. Use a composite component instead. You can use <cc:attribute method-signature> to define action methods as attributes.

    <cc:interface>
        <cc:attribute name="method" method-signature="void method()"/>
    </cc:interface>
    <cc:implementation>
        <p:commandButton action="#{cc.attrs.method}" value="Submit"/>
    </cc:implementation>
    

    Which is used as follows:

    <my:button method="#{managedBean.someAction}" />
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm sorry, the method was actionListener. And since I'm forced to use primefaces, I don't have a choice of using omnifaces. Any other choices or options, please? – sciFi Feb 21 '13 at 12:54
  • 3
    Just change `action` in answer by `actionListener` it you really need to? Why do you think that PrimeFaces and OmniFaces can't be used together? OmniFaces is an utility library, not a component library like PrimeFaces/RichFaces. Even more, the OmniFaces showcase application uses PrimeFaces for UI. Note that I mentioned 3 options, of which 2 don't use OmniFaces. Also note that OmniFaces is open source under Apache License. – BalusC Feb 21 '13 at 12:55
  • `@balus` Is it that, when a bean's method is mentioned inside the src of `` then that method is always called before other methods (when rendering a page)? – sciFi Feb 22 '13 at 05:57
  • @@balusC Sadly, I have tried them, I cant still access the method in my actionListoner. Am I committing a basic mistake? Please help. – sciFi Feb 22 '13 at 07:25