Passing actions to composition components

In the recipe Creating composition components in JSF 2.0, we passed different types of values to our composition component. In this recipe, we take a step forward and pass an action to it, instead of explicitly mapping the action in the composition component.

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

The solution is based on two steps:

  1. We pass the action name as shown next (focus on the action attribute):
    …
    <x:tableColumn book="${bk}" attr="title" action="sortBooks"
    compbean="${booksStore}" />
    …
    
  2. We use the passed action in the composition component:
    …
    <h:commandLink action="#{compbean[action]}">
    <h:outputText value="ascending" />
    <f:param name="by" value="${attr}"/>
    <f:param name="order" value="ascending"/>
    </h:commandLink>
    …
    

That's all! Now you should be able to pass an action binding to create different elements such as toolbars.

How it works...

As the standard EL can't help us here, we have used a little trick supported by Facelets—we have referenced the value binding in a generic way!

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Pass_actions_to_composition_components.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset