Inside facelets - Part 3: Templating and re-use deep into the Facelets - Part III: Create a template and reuse by javafeng
This is the third part in the Facelets series, which is to create a JSF application with another view technology. Facelets is a powerful template system that allows you to define a JSF view with an HTML style template and can reduce the number of code suitable for integrating components in the view and do not require a web container. This article explains how you can create a template in your JSF project with Facelets.
For any view technique for success, it must have some functions that create templates and reuse, and must be easy to understand. For JSF, Facelets technology has perfectly solved this problem and continues the traditional, label-based UI style. This article consists of some ways to increase reuse and simplify maintenance JSF projects.
When people start to create a web page for the first time, they often find themselves to repeat the same content in multiple files. As a developer, when you start using the object-oriented thought to develop, this repeated labor will be very disclaimed. Do you simply maintained in a place not more beautiful?
Start
The first step in template and reuse is to create a template. A web page is usually consisting of some basic parts: Header, Body, and Footer. With Facelets, you can put these universal elements in a separate page and create a template with editable zone, as shown in the following template:
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
XMLns: ui = "http://java.sun.com/jsf/facelets">
hEAD>
body>
html>
For MENU and Body, the
ui: composition>
This example introduces another tag
In order to match the Name property of the content and template pair, the
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
XMLns: ui = "http://java.sun.com/jsf/facelets">
hEAD>
here's my body p>
body>
html>
The previous example can be more simplified, if only one place in the template is variable.
...
body>
...
Want to simplify the page with the top template:
Here's My Simple Body
ui: composition>
Use incrudes
The location in the page This concept defines reusable content in the page is quite powerful. Several examples show how to display the content in the template. But what if you want to include some things from another page? There is a series of form controls or a complex menu that you would rather separate into a different file in order to reuse or manage each corner without sanding. In general, Facelets provides two ways to include location or other pages. The first is to use the
...
span>
...
.
...
t: Tree2>
ui: composition>
...
When Facelets processes include.xhtml, content in all
...
...
t: Tree2>
span>
...
If you are willing to deliver variables for SiteNav.xHTML, these variables are used for Tree components, then you can use the
...
ui: incrude>
span>
...
...
...
t: Tree2>
ui: composition>
...
You can see that Sitenav.xHtml can use the variable MenuBean and MenuBean is passed through the
For
tag>
facelet-taglib>
In addition to making Facelets in your new tag library XML file, you may need to add as many labels as possible in the new label library. You can write this by specifying http://www.mycompany.com/jsf as a namespace.
...
span>
...
This include-tag.xhtml example is the same as the previous include.xhtml example. Menubean's properties will take effect as a attribute of SiteNav.xHtml, just like
Your project may include a JAR file, including all custom label libraries, or simply put the custom label library in a file plus. More tags are described in detail in Facelets Developer Documentation.