Spring Book – Chapter 11 – Spring Web


Spring Faces

Spring Faces is Spring’s JSF integration module. Spring Web Flow supports JSF (JavaServer Faces) technology through the Spring Faces module. Spring Faces lets you use JSF as a view technology in Web applications, with the Spring MVC Web framework underneath. In the form of Spring Faces, Spring Web Flow addresses some of the problematic issues posed by the JSF framework, but it also gives you advanced features as part of JSF 2.0 specification. It provides support for Web Flow in a JSF environment, as well as a lightweight component library for progressive Ajax built on Spring JavaScript. Inline with JSF specification, it provides complete support for developing component-based web application.

JavaServer Faces (JSF) is a component-based view technology in the Java EE ecosystem. It includes a set of predefined UI components, an event-driven programming model, and the ability to add third-party components. Its designed to be extensible, toolable and easy to use.

Spring has long included basic JSF integration in its web module which definitely provides the necessary bond between a typical JSF view layer and a Spring business layer. This is a JSF-centric way of integrating the two technologies. Even though integration is good, there is some disconnect which Spring Faces tries to fill in. It brings in strengths of JSF to a Spring driven web development. JSF itself provides many extension points which Spring Faces takes full advantage to fit JSf more naturally into the Spring ecosystem. It also integrates well with the Facelets templating engine and all popular JSF component libraries.

Facelets is an open source Web template system under the Apache license. It is the default view handling technology for JSF. 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.


As opposed to conventional JSF frameworks, Spring Faces has some distinctive features which can be summarized as follows:

  • No faces-config configuration required – just by putting the jar namely spring-faces.jar in your classpath enables all features for you.
  • Has all the powers of Spring Web Flow
    • Modular and rich page flow definitions
    • Easily get conversational and intra-page state management
    • The UI element is directly backed with the Spring bean
    • Progressive development support makes presenting different pages components according to the client’s capability.

Configuring Spring Faces in Application

The various configurations needed for configuring Spring Faces into your application can be divided into following steps:

  • Configure JSF framework
  • Configure Facelets
  • Configure Spring Application Context

The various configuration files which needs to be configured are; web.xml, faces-config.xml and applicationContext.xml. These configurations will be covered in detail in the following sections.

Configure JSF framework

Configuring JSF framework is done in the web deployment descriptor file; web.xml. Listing 11-6 shows configuring main JSF servlet namely FacesServlet in web.xml file. The servlet is configured in such a way that it will be loaded on application startup.

Listing 11-6. JSF servlet FacesServlet configuration in web.xml


<servlet-name>Faces Servlet</servlet-name>





<servlet-name>Faces Servlet</servlet-name>



The actual configuration of JSF is done in the faces-config.xml file inside the WEB-INF directory of your web application as shown below in Listing 11-7.

Listing 11-7. Configuraing JSF in faces-config.xml file

[xml]<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC

"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"




<!– Enables Facelets as the view layer –>




Configure ResourceServlet

Using this servlet residing in org.springframework.js.resources package, you have provision of accessing files like JavaScript and CSS packaged inside a jar (Java Archive) file over the HTTP protocol. The configuration of this servlet is also done in web.xml as shown in Listing 11-8 below.

Listing 11-8. Configuring ResourceServlet in web.xml


<servlet-name>Resource Servlet</servlet-name>





<servlet-name>Resource Servlet</servlet-name>



Configure Facelets

To use the Spring Faces component library, you need to configure Facelets. Configuring Facelets is done in the web.xml file as shown in Listing 11-9 below.

Listing 11-9. Configuring JSF to use Facelets as view layer in web.xml





The configuration is done using context parameter namely “javax.faces.DEFAULT_SUFFIX” and thevalue being “.xhtml”.

As opposed to conventional JSP, Facelets technology offers the following features:

  • Similar to Tiles Framework, templating mechanism
  • Components can be combined together to form new components, which is very handy and maintainable.
  • Usage of expression functions is supported
  • Possibility of using HTML for your pages
  • HTML being used, it is easy to create them and view them in any browser. This also avoids server need to see your designed pages in action.
  • Possibility of creating libraries out of your own components and distributing it

With these features the view layer in the new JSF 2.0 is defaulted to Facelets replacing earlier view layer namely JSP.

Apache Tiles is a templating framework built to simplify the development of web application user interfaces. Tiles allow authors to define page fragments which can be assembled into a complete page at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.

Configure Spring Application Context

The next task is to configure the Spring application context. Before that we need to specify in the web.xml the application context file name and its location. Listing 11-10 shows configuring application context file name and its location in web.xml file.

Listing 11-10. Configuring application context file details in web.xml








Now that the web.xml configuration is complete, we will now configure spring application context. The various steps involved can be summarized as below:

  • Define appropriate XSD declarations

[xml]<beans xmlns="http://www.springframework.org/schema/beans"







  • Spring Web Flow configuration
    • Configure builders – For the integration between JSF and Spring you need to configure parsers which creates appropriate builders. It can be done in the application context configuration file as follows:

[xml]<faces:flow-builder-services id="facesFlowBuilder"/>[/xml]

  • Configure Flow Registry – For Spring Web Flow to work, it need a flow registry where in the flow definition file details are specified. It can be done in the application context configuration file as follows:

[xml]<webflow:flow-registry id="flowRegistry"


<webflow:flow-location path="/WEB-INF/flows/flowOne.xml" />


In Chapter 13 and Chapter 14, we will be covering the various details of these configuration used in various listings in detail.

Page Visitors: 2699

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 11 – Spring Web

Leave a Reply

Your email address will not be published. Required fields are marked *