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
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
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
<!– Enables Facelets as the view layer –>
Listing 11-8. Configuring ResourceServlet in web.xml
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
- 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:
- 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:
<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: 2417