Facelets

{{short description|Open-source Web template system}}

{{Infobox software

| name = Facelets

| latest release version = 2.0

| latest release date = {{release date|2009|06|28}}

| operating system = Cross-platform

| programming language = Java

| genre = Web template system

}}

{{Infobox software

| name = Facelets standalone

| logo =

| screenshot =

| caption =

| developer =

| latest release version = 1.1.15

| latest release date = {{release date|2009|11|24}}

| latest preview version = 1.2-dev

| latest preview date = {{release date|2006|11|10}}

| operating system = Cross-platform

| programming language = Java

| size = 5.07 MB (archived)

| genre = Web template system

| license = Apache License 2.0

| website = {{URL|http://facelets.java.net/}}

}}

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"{{cite web |last=Bergsten |first=Hans |date=2004-06-09 |df=mdy |title=Improving JSF by Dumping JSP |url=http://onjava.com/pub/a/onjava/2004/06/09/jsf.html |work=ONJava |publisher=O'Reilly Media |archive-url=https://web.archive.org/web/20180405081920/http://onjava.com/pub/a/onjava/2004/06/09/jsf.html |archive-date=2018-04-05}}

Facelets draws on some of the ideas from Apache Tapestry,{{cite web |url=https://facelets.java.net/nonav/docs/dev/docbook.html |title=Facelets: JavaServer Faces View Definition Framework |work=java.net |archive-url=https://web.archive.org/web/20161231170917/https://facelets.java.net/nonav/docs/dev/docbook.html |archive-date=2016-12-31}}{{cite web |last=Hookom |first=Jacob |date=2005-08-17 |df=mdy |title=Inside Facelets Part 1: An Introduction |url=http://www.jsfcentral.com/articles/facelets_1.html |work=JSFCentral |archive-url=https://web.archive.org/web/20200105063529/http://www.jsfcentral.com/articles/facelets_1.html |archive-date=2020-01-05}} 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 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.{{cite book |last1=Burns |first1=Ed |last2=Schalk |first2=Chris |date=2009 |title=JavaServer Faces 2.0, The Complete Reference |publisher=McGraw-Hill |isbn=978-0-07-162509-8 |quote-page=55 |quote=The expert group decided to move forward with Facelets as the basis for new features while letting JSP remain as a backward compatibility layer.}}{{cite tech report |editor-last1=Burns |editor-first1=Ed |editor-last2=Kitain |editor-first2=Roger |date=2010-11-08 |df=mdy |title=JavaServer Faces Specification, Version 2.1 |url=https://jcp.org/en/jsr/detail?id=314 |edition=MR2 |work=JCP |institution=Oracle |number=JSR-314 |quote-page=10-1 |quote=Facelets is a replacement for JSP that was designed from the outset with JSF in mind. New features introduced in version 2 and later are only exposed to page authors using Facelets. JSP is retained for backwards compatibility.}}

Element conversion

In Facelets, templates tags from a tag library can be entered in two 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 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 {{cite web|url=https://stackoverflow.com/questions/tagged/jsf|title=Newest 'jsf' Questions|work=stackoverflow.com|accessdate=22 November 2016}} and is the style most used in books and examples.JavaServer Faces 2.0, The Complete Reference by Ed Burns and Chris SchalkCore JavaServer Faces (3rd Edition) by David Geary and Cay S. Horstmann

Templating

Facelets provides a facility for templating.{{cite web|url=http://www.ibm.com/developerworks/java/library/j-jsf2fu2/index.html|title=JSF 2 fu, Part 2: Templating and composite components|date=2 June 2009|work=ibm.com|accessdate=22 November 2016}}{{cite web|url=http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ui/tld-summary.html|title=ui (JSF 2.0 Page Decraration Language: Facelets Variant)|work=oracle.com|accessdate=22 November 2016}} 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 that 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 three 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.{{cite web|url=http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ui/include.html|title=include (JSF 2.0 Page Decraration Language: Facelets Variant)|work=oracle.com|accessdate=22 November 2016}}{{cite web|url=http://pilhuhn.blogspot.com/2009/12/facelets-uiinclude-considered-powerful.html|title=Some things to remember: Facelets ui:include considered powerful|date=4 December 2009|work=pilhuhn.blogspot.com|accessdate=22 November 2016}} 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 into 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 for including content via custom tags.{{cite web|url=https://matthiaswessendorf.wordpress.com/2008/02/29/custom-jsf-components-with-facelets/|title=Custom JSF components with Facelets|date=29 February 2008|work=wordpress.com|accessdate=22 November 2016|archive-url=https://web.archive.org/web/20161019091617/https://matthiaswessendorf.wordpress.com/2008/02/29/custom-jsf-components-with-facelets/|archive-date=19 October 2016|url-status=dead}} 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

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-facelettaglibrary_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.{{cite web|url=http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/composite/tld-summary.html|title=composite(JSF 2.0 Page Decraration Language: Facelets Variant)|work=oracle.com|accessdate=22 November 2016}}{{cite web|url=https://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/|title=What's New in JSF 2?|date=31 July 2009|work=wordpress.com|accessdate=22 November 2016}} 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.JSR 314, 10.3.3.1, http://jcp.org/en/jsr/detail?id=314 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,{{cite web|url=http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ui/param.html|title=param(JSF 2.0 Page Decraration Language: Facelets Variant)|work=oracle.com|accessdate=22 November 2016}} 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,{{cite web|url=http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/composite/interface.html|title=interface (JSF 2.0 Page Decraration Language: Facelets Variant)|work=oracle.com|accessdate=22 November 2016}} 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.

See also

{{Portal|Free and open-source software|Computer programming}}

References

{{Reflist}}

Bibliography

{{Refbegin}}

  • {{Cite book

|first1 = Zubin

|last1 = Wadia

|first2 = Martin

|last2 = Marinschek

|first3 = Hazem

|last3 = Saleh

|first4 = Dennis

|last4 = Byrne

|date = September 22, 2008

|title = The Definitive Guide to Apache MyFaces and Facelets

|edition = 1st

|publisher = Apress

|pages = 400

|isbn = 978-1-59059-737-8

|url = http://www.apress.com/book/view/9781590597378

|access-date = September 4, 2009

|archive-url = https://web.archive.org/web/20100102125147/http://apress.com/book/view/9781590597378

|archive-date = January 2, 2010

|url-status = dead

}}

  • {{Cite book

|first1 = Zubin

|last1 = Wadia

|first2 = Bruno

|last2 = Aranda

|date = May 26, 2008

|title = Facelets Essentials: Guide to JavaServer Faces View Definition Framework

|edition = 1st

|publisher = Apress

|pages = 84

|isbn = 978-1-4302-1049-8

|url = http://www.apress.com/book/view/9781430210498

|access-date = September 4, 2009

|archive-url = https://web.archive.org/web/20100108044551/http://apress.com/book/view/9781430210498

|archive-date = January 8, 2010

|url-status = dead

}}

{{Refend}}