Sunday Feb 07

Key Concepts


Java Server Faces 2.0

AddThis Social Bookmark Button


JavaServer Faces (JSF Faces) is a standardized specification for building User Interfaces (UI) for server-side applications. Before JavaServer Faces, developers who built web applications often relied on building HTML user interface components with servlets or JavaServer Pages (JSP pages).

JavaServer Faces (JSF) is a user interface (UI) framework for Java web applications. It is designed to significantly ease the burden of writing and maintaining applications that run on a Java application server and render their UIs back to a target client.

JSF provides ease-of-use in the following ways:

  • Makes it easy to construct a UI from a set of reusable UI components 
  • Simplifies migration of application data to and from the UI 
  • Helps manage UI state across server requests 
  • Provides a simple model for wiring client-generated events to server-side application code 
  • Allows custom UI components to be easily built and re-used

Foundational new features in JSF 2.0

These features were developed by the expert group specifically to enable other features in JSF 2.0

  • System Events provides a very fined-grained level of detail to observe and act upon the JSF runtime as it processes requests. It is described in detail in Chapter 9.
  • Resources feature allows the JSF runtime to serve up static resources, such as style sheets, scripts, and images, in addition to the previously available capability to serve up JSF pages. 
  • Facelets began as an open-source JSF extension that provided first-class templating and easy integration between markup and the JSF API. This made the experience of writing JSF pages easier and more maintainable. Facelets is now fully included in the core JSF specification in version 2.0 

New Features in JSF 2

  • Ajax JSF 2 provides now a native support for Ajax. Ajax is an essential element of most production-quality Web applications currently in production. Ajax enables JSF views to communicate with the server directly from the browser without requiring a full-page refresh of the browser window.
  • Composite Components JSF 2 provides a simple way to implement custom components with composites. JSF 2 lets you implement user interfaces that are easy to modify and extend with two powerful features: templating and composite components


Last Updated on Thursday, 26 May 2011 15:50 Thursday, 26 May 2011 15:43
Attention: open in a new window. PDFPrintE-mail
AddThis Social Bookmark Button


Url rewriting is not reserved to PHP solutions. JSF has now a library able to provide SEO features like :  

  • URL Rewriting — Replace ugly URLs: “/faces/page.jsf” with pretty, SEO-friendly, human URLS: “/optimized/page/”; The client browser never sees your original URLs, even in the source-HTML 
  • Dynamic view IDs — Now with dynamic view ID mapping for URLs (through El method expressions,) you can show different views based on run-time conditions, effectively de-coupling the URL from the JSF view. 
  • Page-load actions — Call one or more action-methods, at one or more phases, before rendering a view. 
  • Managed parameter parsing — HTTP parameters parsed from URLs are stored in managed beans, simply get a reference to your parameter bean for easy access from other Java classes. 
  • Navigation by ID — Define your page IDs in one place, reference them easily in action-methods and components, using standard JSF navigation techniques. 
  • Integrated validation — For URL and query-parameters, able to re-use existing JSF validator objects. 
  • Easy to configure — Requires absolutely minimal configuration; may be enabled using XML or annotations.

Prettyfaces is that library.


PrettyFaces is an OpenSource extension for Servlet, Java EE, and JSF, which enables creation of bookmarkable, REST-ful, "pretty" URLs. PrettyFaces solves several problems elegantly, such as: custom URL-rewriting, page-load actions, seamless integration with JSF navigation and links, dynamic view-id assignment, managed parameter parsing, and configuration-free compatibility with other JSF frameworks.

Maven dependencies configuration 

Include the following snippet in the <dependencies> section of your project's pom.xml-file:

Maven plugin configuration

<!-- For Servlet/Java EE and JSF 2.0 -->

Last Updated on Monday, 26 September 2011 09:43
Monday, 26 September 2011 08:55
AddThis Social Bookmark Button

JSF 2 Bean Validation (JSR 303)

Bean Validation with JSR 303 

A common issue when receiving data either electronically or from user input is verifying the integrity of the data. This work is repetitive and becomes even more complicated when different sets of validation rules need to be applied to the same set of data based on locale. Error messages may also vary by locale. The JSR 303 addresses some of these issues to speed development and maintenance of validation rules.


alidating data is a common task that occurs throughout an application, from the presentation layer to the persistence layer. Often the same validation logic is implemented in each layer, proving to be time consuming and errorprone. To avoid duplication of these validations in each layer, developers often bundle validation logic directly into the domain model, cluttering domain classes with validation code that is, in fact, metadata about the class itself.

The JSR 303 defines a metadata model and API for JavaBean validation. The default metadata source is annotations, with the ability to override and extend the meta-data through the use of XML validation descriptors.

The validation API developed by this JSR is not intended for use in any one tier or programming model. It is specifically not tied to either the web tier or the persistence tier, and is available for both server-side application programming, as well as rich client Swing application developers. This API is seen as a general extension to the JavaBeans object model, and as such is expected to be used as a core component in other specifications. Ease of use and flexibility have influenced the design of this specification.

Source :  jsr 303 specifications

You can download the specifications on the JCP site 

JSR 303 implementations

Provider Name Version Download
Hibernate  hibernate-validator (API)


Donwload binary 

Code Google GWT-Vadliation


Download binary
Code Google Wicket-jsr303-Validators

Download binary

What is not 

  • JSR 303 is not designed for Cross validation

JSF 2 Expression Language Tips and Tricks

Last Updated on Tuesday, 05 February 2013 10:48 Thursday, 24 January 2013 17:58
Attention: open in a new window. PDFPrintE-mail
AddThis Social Bookmark Button

Java Server Faces 2.0 Expression Language Tips and Tricks

Java Server Faces 2 Expression Language Tips and Tricks

Put the View id in the title only in development

JSF 2.0 has introduced a new feature called project stage. The project stage define the role of the current deployment. You can define this parameter in your Web.xml configuration file. Sometimes during the development you need to know which source file is behind the current page but you want to display this debugging information only in the development stage. To achieve this target you can use the following snippet:

Display page view id in development stage using JSF 2.0

        <title>Java tutorial
            <ui:fragment rendered="#{initParam['javax.faces.PROJECT_STAGE'] eq 'Development'}">
                 <h:outputText value="[#{view.viewId}]"/>

Display the current page View Id or URI 

The viewId is a context-relative path starting with a slash character indicating the location of the source file


<h:outputText value="#{view.viewId}"/>

The result should display something like : 


To display the URI of your page, you can use the following snippet


<h:outputText value="#{request.requestURI}"/>

The result should display something like : 


Resource Handler

If you want to get access to your resource handler your can use the following snippet

<h:outputText value="#{resource}"/>

It can be used to display debugging information. You can ask if a library exists :

<h:outputText value="Is my YAML css add-ons existings"/>
<h:outputText value="#{resource.libraryExists('css/yaml/add-ons/microformats')}"/>

If this examples i'm checking if the YAML Microformats add-ons is present.

If you have a custom resource handler.It has been defined in your faces-config.xml file with the following xml definition :


If you have any remark or questions feel free to put a comment.

If you enjoyed this tutorial and want to promote it don't hesitate to click on

Last Updated on Tuesday, 29 May 2012 15:26
Tuesday, 17 May 2011 17:32
AddThis Social Bookmark Button

Create a composite tag library 

The design of JSF 2.0 is influenced by the philosophy of convention over configuration, popularized by Ruby on Rails. To realize composite components, this philosophy is combined with the resource loading feature, described in Ryan Lubke’s blog JSF 2.0 New Feature Preview Series (Part 2.1): Resources.

The namespace short-name that prefixes a composite component tag, ubiteck:, has an entry in the element, that is, xmlns:ubiteck="". The content after in this case, ubiteck, is the library-name. The library-name and resource-name are used to create a Resource instance.

JSF 2.0 has a method that creates a UIComponent from a Resource. Due to these conventions, any .xhtml file in a resource library can be accessed as a composite component.

There are several ways to create a resource library. Here is one way to do it.

In your webapp directory, you should have a directory named resources. In this directory is a sub-directory named ubiteck. The name of the subdirectory (in this case, ubiteck) must correspond to the end part of the composite xmlns for the namespace short-name that prefixes the composite component tag. 

Namespace for composite A Composite namespace should start with<Name of your library>
Property access  

A property defined in the interface section should be accessed using 
#{cc.attrs.<name of your property>}

Structure content under
these resources directories

items in [] are optional

associate a resource with a particular locale identifiter

If a developer wishes to 
leverage this feature, they must add a key/value pair like: 

  • javax.faces.resource.localePrefix=<localePrefix>

The default implementation will look for resources in two locations and in the following order:

  1. /resources this location represents resources in the webapp itself and if present, must be in the root of the web application 
  2. /META-INF/resources this location represents resources on the classpath

HTML Spacer composite component example

Let's imagine we want to create a spacer component. We will create a file called spacer.xhtml in the folder ../webapp/resources/ubiteck/

The spacer take support a width parameter to define  the length of the spacer. The default value if omiited is 10 pixels.  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""

    <title>Spacer Ubiteck component</title>
      Usage  :  
        <md:spacer width="50"/>
            <composite:attribute name="width" defaut="10" />
                width="#{cc.attrs.width}" height="1" />

The graphic image is tag is more described at Tag Graphic Image  (JSF 2.0)

The component will be reachable defining the namespace for ubiteck and calling <ubiteck:spacer width="100"/> and passing the width parameter

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
    <title>Spacer Composite Components Test</title>
    Before spacer <ubiteck:spacer width="100"/> After Spacer        
<meta charset="utf-8" />

Migrate tag library from 1.x to 2.x

Find below a small table with correponding tags or expressions between JSF 1.x and JSF 2.x

TagsJSF 1.xJSF 2.x
A property attribute  myParam cc.attrs.myParam

Any child components or template text within the composite component
tag in the using page will be reparented into the composite component



method param N/A
name="<param name>"
method-signature="java.lang.String action()"/>
Bean + method 
JSTL Choose


<c:when test="<condition>">



JSTL If conditional

<c:if test="<condition>">

<f:verbatim rendered="<condition>">

Last Updated on Tuesday, 17 May 2011 17:17
Wednesday, 11 May 2011 06:48
AddThis Social Bookmark Button

Unified Expression Language 2.2 

EL-2.2 is part of JSR-245 MR2 (JSP-2.2) and therefore not included in Tomcat6 nor Jetty6. EL-2.2 is part of Tomcat7 and Jetty7, but with a few manual steps, you can use EL-2.2 even in current containers.

Add Unified expression language in your Java server faces project using Maven

Include the following snippet in the dependencies section of your project's pom.xml-file to get EL2 :

javax.el dependencies maven


Include the repository project's pom.xml-file or in your settings.xml :

Unified expression language repository download location

   <url> </url>

Apache's EL 2.2 implementation

The above code uses Sun's EL 2.2 implementation from Glassfish. If you want to use Apache's implementation, you have to use the following dependencies from the Apache SNAPSHOT repository.

Include the following snippet in the <dependencies> section of your project's pom.xml-file:

Apache EL 2.2 Maven configuration


web.xml settings

Include the following snippet in the <build><plugins> section of your project's pom.xml-file:


Tomcat 6 Configuration for EL 2.2

Tomcat6 has to be prepared, otherwise it will crash with a Exception on container startup:

1 ) Go to the tomcat home directory 

2) Remove tomcats el-api.jar from the classpath:

Tomcat6 el-api el-impl Expression Language

> mv ./lib/el-api.jar ./lib/el-api.jar.bak

3) Copy the EL-2.2 jars into ./lib (el-api-2.2.jar, el-impl-2.2.jar) 

4) Make sure that your WARs don't contain those 2 el jars!

Java Tutorial on Facebook