Search code examples
jsfprimefacesuiinclude

Command Button inside composition page in JSF


I have the same problem as user1598186 has stated in his question here : p:commandButton doesn't call bean's method in an <ui:include> page

However, no solution has been given (he has removed <ui:include> tags altogether and used variables instead)

Are there any ways of using <ui:include> and still have my backing bean's method executed, when I'm calling it inside the commandButton.

Any help will be much appreciated.


Solution

  • EL 2.2 method parameters (so, #{bean.method()} instead of #{bean.method}) can be used to pass a method signature that can be used in the actionListener attribute of a commandButton. The following is an example of passing a ManagedBean property as well as passing a method signature:

    Main Page

    <ui:include src="/jointeam.xhtml">
      <ui:param name="propertyValue" value="#{managedBean.property1} />
      <ui:param name="method" value="#{managedBean.performAction()}" />
    </ui:include>
    

    jointeam.xhtml

    ...
    
    <h:inputText value="#{propertyValue}" />
    
    ...
    
    <p:commandButton value="Submit" actionListener="#{method}" />
    

    You can see how powerful this is in terms of code reuse and for many instances is less verbose and easier to use than composite components.