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