Passing sub-elements to composition components

First of all, you need to keep in mind that this recipe uses the knowledge and code from the previous recipe, therefore it is recommended to read the previous recipe first!

Now, focusing on this recipe, you should know that the key to its design lies in the fact that a Facelets composition component is actually a type of template. Based on this important observation, we can pass a template argument using the ui:define (associated to a corresponding ui:insert). Also, we can pass the body as a default ui:insert.

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...

First, we place an anonymous ui:insert in the composition component (you should place it exactly in the place where you need it to be replaced by Facelets) For example, we place it in each table column as shown next:

…
<h:outputText value="${book[attr]}" />
<ui:insert />
</h:column>
</ui:composition>
</html>

Now, when we invoke the composition component, the anonymous insert introduces the passed body. If the body is present, then nothing is introduced. In the following example, we are using a body for author and price columns (our body is just an f:verbatim component, but it can be anything else).

…
<x:tableColumn book="${bk}" attr="title" compbean="${booksStore}" />
<x:tableColumn book="${bk}" attr="author" compbean="${booksStore}">
<f:verbatim> [*****]</f:verbatim>
</x:tableColumn>
<x:tableColumn book="${bk}" attr="price" compbean="${booksStore}">
<f:verbatim> - Promotion!</f:verbatim>
</x:tableColumn>
…

Now the rendered table looks similar to the following screenshot:

How to do it...

How it works...

Well, as we said in the description of the recipe, the secret lies in the fact that a Facelets composition component is acting like a template. I think that this observation says it all!

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_sub_elements_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