The beauty of Spring is that it can integrates well with most of the prevailing popular technologies, and in the case of ORM, it integrates well with Hibernate.
I have written a Spring Book and in Chapter 8 i explain Spring’s Data Access capabilities in detail. There is separate topics written on ORM integration and this specific heading namely “Spring and Hibernate” is dedicated for explaining this beautiful integration between two open-source framework.
This section of the chapter examines the means by which Spring can be integrated with Hibernate. I assume that the reader is somewhat familiar with Hibernate and by reading this far familiar with Spring Framework as well to some extend.
ORM is a piece of software/product for the representation and conversion of data between the database and the object-oriented programming language. Hibernate is a powerful technology for persisting data and is a clear winner in the ORM race as of now. However it hasn’t become a standard. It takes much of the database related boiler-plate code from the developers, thereby asking the developers to concentrate on the core business logic of the application and not with the error-prone SQL syntax. It also enables application to access data from any database in a platform-independent manner.
One of the problems with using Hibernate is that the client Application that accesses the database using Hibernate Framework has to depend on the Hibernate APIs. These objects will continue to get scattered across the code throughout the Application. Above all, the application code has to manually maintain and manage these objects. Here comes Spring to the rescue with its IoC container capable of configuring these objects using Spring configurations.
JSF being one of the very successful, prominent and well backed up web framework, the importance of integration between Spring and JSF is covered in full detail with meaningful pictures and example code in this chapter for readers who are beginners and experts alike.
JavaServer Faces (JSF) is a server side user interface component framework for developing Java technology-based web applications. Developed through the Java Community Process under JSR – 314, it establishes the standard for building server-side user interfaces and have gained tremendous acceptance over the period of time in the Java world. JavaServer Faces APIs are being designed keeping in mind that they can be leveraged by tools that will make web application development even easier and simpler.
This Chapter provides an introduction on how the in famous Spring Framework can be integrated with in famous web framework, Java Server Faces Technologies. The Chapter covers the necessary details of both the Spring Framework and JSF technology in the initial section in the context of Integration and then later on moves towards the concept of concepts which help in easing the integration between the two technologies.
Before getting into the details of how Sping and JSF Integration, it is important to know basic details of JSF. The following initial sections of this Chapter will cover JSF in some detail so that initial context is set and we can understand the integration in a clear fashion.
JSF is based on Model-View-Controller (MVC) architecture. Since it is based on well know MVC design the applications can be designed in a simpler and easy fashion keeping maintainability factor intact. In earlier Chapters we have covered MVC design patter in detail, so I wouldn’t repeat the same in this Chapter.
MVC model’s main purpose is to separate model and presentation to enable developers to set focus on their core skills and collaborate more clearly. If the application demands creating different pages for presentation, developers can concentrate on the view layer rather than model and controller layer as developers have a provision to reuse code for controller and model already written with ease.
Pictorially JSF architecture keeping the essence of MVC design can be represented as shown in Figure 13-1 below.
Figure 13-1. JSF architecture showing off MVC design
The processing of a request is called lifecycle in JSF. There are different phases which are executed one after the other following “Chain of responsibilities” design pattern. Figure 13-2 below shows the JSF lifecycle.
Figure 13-2. JSF lifecycle
There are various phases constituting the JSF lifecycle. The various lifecycle phases can be summarized as follows:
Apply Request Values
Update Model Values
Pictorially the various phases in the JSF lifecycle can be as shown in Figure 13-3 below.
Figure 13-3. JSf lifecycle phases
JSF application in many ways work very much similar to a typical desktop application comprising of components and events. In line with client application, components can create events and can react or listen to events. There are different types of events and there are various places where these events are created and invoked. The events can be classified into three different types as shown below:
System events – These events are created at various stages in an application or request lifetime. These events can be configured to evoke globally to all components and others which are related to a component.
Phase events – Phase events are coarse grained in nature and are broadcasted before and after every phase. You can use phase listener to check access before a phase is entered. You also have a provision of registering global phase event listener and per view phase event listener. Global phase listener are executed for all requests whereas per view listener only for a request of the specific view.
Application events – Application events are created by the application components. JSF has two types of events namely:
Figure 13-4 below pictorially shows the events which are invoked at the various phases in the JSF lifecycle.
Figure 13-4. JSF lifecycle phases along with event invocation points
My humble effort to explain Spring and Struts integration in detail using code samples for easy understanding for my fellow developers and readers.
Apache Struts is more established, more used and well known MVC2 web framework in the Java world. I am sure anyone experienced with web application will surely have had a chance to work with this one of the pioneers in web framework.
Over the past few years there have been so many buzz words in the world of software development like Inversion of Control (IoC), AOP etc. These paradigms has revolutionized the software development and Spring being one of the frameworks adopting these new technologies into it and have become really good at it.
This Chapter makes a big assumption that the reader is conversant with the Struts Framework and that the reader would like to integrate the Spring Framework into the struts application so as to use the capabilities and out-of-box features provided by the Spring Framework.
If you like to take advantage of these features of Spring you do not have to rebuild the application, but you can integrate your existing Struts application with Spring without much hassle. This Chapter will let you do exactly this so that you can take the advantages of both worlds with good level of confidence and comfort. This Chapter is a very simple one and I am sure you will be able to complete it at one go without trouble whatsoever.
Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.
Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.
Apache Struts in an MVC2 Java web framework. MVC2 is a servlet-centric architecture. MVC2 incorporates a clear separation of view and controller. A controller receives all the requests, retrieves data from a Model and forwards it to next view for presentation.
In MVC2, there is a central Servlet (Controller) which calls business logic (Model) and the forwards it particular JSP page (View) that is why it is also called servlet-centric architecture. Pictorially MVC2 implementation is as shown in Figure 12-1 below.
Figure 12-1. MVC2 (Servlet-centric architecture)
WebWork was a Java-based web application framework developed by OpenSymphony which got merged into the current Struts2 web application framework. WebWork is built on top of XWork, which provides a generic command pattern framework as well as an Inversion of Control container.
Advantages of integrating Struts with Spring
The advantages of integrating a Struts application into the Spring Framework are:
Spring framework is based on new design approach and was designed to resolve the existing problems of existing Java applications. To a larger extent Spring has been successful in living to the expectations. Integrating Struts application with Spring buys in all the advantages provided out-of-box by Spring.
Spring framework lets you apply AOP (aspect-oriented programming technique) rather than object-oriented code. This is quite useful in addressing some very core concerns in any modern day enterprise application.
Spring provides more control on struts actions. That may depend on the method of integration you choose which we will cover in detail in the following sections of this Chapter.
Before going through Spring and Struts integration in detail, because of the evolution complexity of Struts, we would have to first go through the Struts history with core focus on the versions. According to the Struts version that you use in your application, the integration methodology changes accordingly.
Struts were originally developed by the programmer and author Craig R. McClanahan and were later taken over by the Apache Software Foundation in 2002. Strut1 with all standard Java technologies and packages of Jakarta assists to create an extensible development environment. However, with the growing demand of web application, Struts 1 does not stand firm and needs to be changed with demand. This leads to the creation of Struts 2, which is more developer friendly with features like Ajax, rapid development and extensibility.
The team of Apache Struts and another J2EE framework, WebWork from OpenSymphony joined hand together to develop an advanced framework with all possible developing features that will make it developer and user friendly.
Even though the new version is Spring 2, we would still cover integration of Spring with Struts 1 as well so that if existing Struts 1 project needs the various capabilities of Spring, they can do so without much hassle.