Facelets
In computing, Facelets is an open-source Web template system under the Apache license and the default view handler technology (aka view declaration language) for Jakarta Faces (JSF; formerly Jakarta Server Faces and JavaServer Faces). The language requires valid input XML documents to work. Facelets supports all of the JSF UI components and focuses completely on building the JSF component tree, reflecting the view for a JSF application. Although both JSP and Faces technologies have been improved to work better together, Facelets eliminates the issues noted in Hans Bergsten's article "Improving JSF by Dumping JSP"[1] Facelets draws on some of the ideas from Apache Tapestry,[2][3] and is similar enough to draw comparison. The project is conceptually similar to Tapestry's, which treats blocks of HTML elements as framework components backed by Java classes. Facelets also has some similarities to the Apache Tiles framework with respect to support templating as well as composition. Facelets was originally created by Jacob Hookom in 2005[3] as a separate, alternative view declaration language for JSF 1.1 and JSF 1.2 which both used JSP as the default view declaration language. Starting from JSF 2.0, Facelets has been promoted by the JSF expert group to be the default view declaration language. JSP has been deprecated as a legacy fall back.[4][5] Element conversionIn Facelets, templates tags from a tag library can be entered in two forms: directly as a qualified The following example shows the direct usage of qualified tags: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
<body>
<h:form>
<h:outputText value="Welcome, #{loggedInUser.name}" disabled="#{empty loggedInUser}" />
<h:inputText value="#{bean.property}" />
<h:commandButton value="OK" action="#{bean.doSomething}" />
</h:form>
</body>
</html>
Using the <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
<body>
<form jsfc="h:form">
<span jsfc="h:outputText" value="Welcome, #{loggedInUser.name}" disabled="#{empty loggedInUser}" />
<input type="text" jsfc="h:inputText" value="#{bean.property}" />
<input type="submit" jsfc="h:commandButton" value="OK" action="#{bean.doSomething}" />
</form>
</body>
</html>
The above code can be viewed in a browser, and edited with conventional WYSIWYG design tools. This is not possible when directly using the qualified tags. Nevertheless, directly using qualified tags is the most popular way of using Facelets in practice [6] and is the style most used in books and examples.[7][8] TemplatingFacelets provides a facility for templating.[9][10] A Facelets file can reference a master template and provide content for the placeholders this master template defines. The file that references such a template is called the template client. Template clients themselves can again be used as a template for other template clients and as such a hierarchy of templates can be created. The following shows an example of a simple master template: templates/master_template.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv="pragma" content="no-cache"/>
</h:head>
<h:body>
Standard header text for every page.
<ui:insert name="body_content" />
Standard footer text for every page.
</h:body>
</html>
The above code contains a default HTML 'frame' and a single placeholder called body_content. A template client can use this template as follows: template_client.xhtml <ui:composition template="/templates/master_template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:define name="body_content">
This is a template client page that uses the master template.
</ui:define>
</ui:composition>
The above code makes use of the template Content re-useIn addition to templating, Facelets provides support for re-use by letting the user include content that resides in a different file. Including such content can be done in three different ways:
Referencing a fileThe simplest way to include the content of another Facelet is referencing it by name using the The following shows an example: templates/master_template.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:include src="html_head.xhtml" />
<h:body>
Standard header text for every page.
<ui:insert name="body_content" />
Standard footer text for every page.
</h:body>
</html>
html_head.xhtml <ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta http-equiv=" pragma" content="no-cache"/>
</h:head>
</ui:composition>
Custom tagsFacelets supports indirection for including content via custom tags.[13] Such a custom tag can be associated with a Facelet in a taglib file. Occurrences of that tag will then be replaced with the content of the associated Facelet. The following shows an example of this: templates/master_template.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:my="http://example.com/my">
<ui:include src="html_head.xhtml" />
<h:body>
Standard header text for every page.
<my:spacer>
<ui:insert name="body_content" />
Standard footer text for every page.
</h:body>
</html>
The code above uses the tag example.taglib.xml <?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://example.com/my</namespace>
<tag>
<tag-name>spacer</tag-name>
<source>spacer.xhtml</source>
</tag>
</facelet-taglib>
The following shows an example of what the actual content Facelet could look like: spacer.xhtml <ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:outputText value="&nbsp;" escape="false"/>
</ui:composition>
Composite componentsBesides including content directly, Facelets provides the composite component mechanism that makes content available as a first-class JSF component.[14][15] Composite components do not need to be declared in a Taglib file, but instead have to be put in a special directory. By convention the content is then automatically assigned a namespace and a tag name. The namespace is constructed of the fixed string 'http://java.sun.com/jsf/composite/' concatenated with the directory name in which the content file resides relative to the 'resources' directory.[16] The tag name becomes the file name without the .xhtml suffix. The following shows an example of this: resources/my/spacer.xhtml <ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface/>
<cc:implementation>
<h:outputText value="&nbsp;" escape="false"/>
</cc:implementation>
</ui:composition>
The above Facelet is automatically available as a component in namespace 'http://java.sun.com/jsf/composite/my' and tag name 'spacer' Parameterized includesTo customize included content, Facelets allows parameters to be used. Via those parameters, objects can be passed into the included content, where they can be used as variables. For the See alsoReferences
Bibliography
External links
|