Facelets
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, Facelets is an open source
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...

 Web template system
Web template system
A Web template system describes the software and methodologies used to produce web pages and for deployment on websites and delivery over the Internet. Such systems process web templates, using a template engine...

 under the Apache license
Apache License
The Apache License is a copyfree free software license authored by the Apache Software Foundation . The Apache License requires preservation of the copyright notice and disclaimer....

 and the default view handler technology (aka view declaration language) for JavaServer Faces
JavaServer Faces
JavaServer Faces is a Java-based Web application framework intended to simplify development integration of web-based user interfaces....

 (JSF). The language requires valid input XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

 documents to work. Facelets supports all of the JSF
JavaServer Faces
JavaServer Faces is a Java-based Web application framework intended to simplify development integration of web-based user interfaces....

 UI components and focuses completely on building the JSF component tree, reflecting the view for a JSF application.

Although both JSP
JavaServer Pages
JavaServer Pages is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types...

 and JSF
JavaServer Faces
JavaServer Faces is a Java-based Web application framework intended to simplify development integration of web-based user interfaces....

 technologies have been improved to work better together, Facelets eliminates the issues noted in Hans Bergsten's article "Improving JSF by Dumping JSP"

Facelets draws on some of the ideas from Apache Tapestry, and is similar enough to draw comparison. The project is conceptually similar to Tapestry's, which converts HTML elements into the corresponding framework components. Facelets also has some similarities to the Tiles framework with respect to support templating as well as composition.

Initially, Facelets was available 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 .

Element conversion

In Facelets templates tags from a tag library can be entered in 2 forms: directly as a qualified xml element or indirectly via the jsfc attribute on an arbitrary non-qualified element. In the latter case the Facelet compiler will ignore the actual element and will process the element as-if it was the one given by the jsfc attribute.

The following example shows the direct usage of qualified tags:


Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











Using the jsfc attribute, the same code can also be expressed as the example given below:


Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











The above code can be viewed in a browser, and edited with conventional WYSIWYG
WYSIWYG
WYSIWYG is an acronym for What You See Is What You Get. The term is used in computing to describe a system in which content displayed onscreen during editing appears in a form closely corresponding to its appearance when printed or displayed as a finished product...

 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 and is the style most used in books and examples.

Templating

Facelets provides a facility for templating. 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

Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>






Standard header text for every page.



Standard footer text for every page.




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

xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
>

This is a template client page that uses the master template.




The above code makes use of the template /templates/master_template.xhtml and provides content for the placeholder in that template. The final result will be a page called template_client.xhtml, which has the content of /templates/master_template.xhtml but with replaced by 'This is a template client page that uses the master template.'.

Content re-use

In 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 3 different ways:
  • Referencing a file
  • Custom tags
  • Composite components

Referencing a file

The simplest way to include the content of another Facelet is referencing it by name using the tag. This causes the content in the referenced file to be directly included in the calling Facelet by the Facelets compiler. Besides re-using content at multiple locations, this can be used to break down a large Facelet in smaller parts.

The following shows an example:

templates/master_template.xhtml

Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>



Standard header text for every page.



Standard footer text for every page.




html_head.xhtml

xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>






Custom tags

Facelets supports indirection
Indirection
In computer programming, indirection is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value through its memory address. For example, accessing a variable through the use of a...

 for including content via custom tags. Such a custom tag can be associated with a Facelet in 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

Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:my="http://example.com/my"
>



Standard header text for every page.




Standard footer text for every page.




The code above uses the tag to mark the point in the Facelet where content is to be inserted. Such a tag has to be declared in a Taglib file where it can be associated with a Facelet as follows:

example.taglib.xml


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-facelettaglibary_2_0.xsd"
version="2.0"
>
http://example.com/my


spacer
spacer.xhtml




The following shows an example of what the actual content Facelet could look like:

spacer.xhtml

xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
>



Composite components

Besides including content directly, Facelets provides the composite component mechanism that makes content available as a first-class JSF component. Composite components do not need to be declared in a Taglib file, but instead have to be put in a special directory. Via convention over configuration
Convention over Configuration
Convention over configuration is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility....

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. The tag name becomes the file name without the .xhtml suffix.

The following shows an example of this:

resources/my/spacer.xhtml

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







The above Facelet is automatically available as a component in namespace 'http://java.sun.com/jsf/composite/my' and tag name 'spacer'

Parameterized includes

To 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 mechanism the can be used for this , while for the custom tags and composite components normal tag attributes can be used. Composite components require parameters to be declared in their interface section , while for custom tags there is no such requirement and values provided for arbitrary attributes are made available as variables with the same name as said attribute.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK