Sunday, March 22, 2009

Integration of JSF and Spring

JSF is a very popular presentation framework for web applications. It has got very rich repository of components as well. Spring help structuring whole application in a consistent, productive manner, pulling together best-of-breed frameworks. Spring enable developers to work with POJOs. Spring is a lightweight container, providing centralized, automated configuration and wiring of application objects.
So JSF in web tier and Spring in middle tier become an obvious choice. This document explains the steps involved in integrating these two frameworks.

Steps involved in integration
1. Add following lines in web.xml


This will load Spring Application context when web application starts.

2. Add applicationContext.xml file in WEB-INF folder
applicationContext.xml is a Spring configuration file. In the below snippet shows JSF managed bean entry.



3. Add following lines to faces-config.xml


When the code sample above is executed then the DelegatingVariableResolver would check to see if "SampleView" exists in spring and if it exists as a JSF managed bean as well. If it does DelegatingVariableResolver will then check to see if the bean already exists in the scope specified by and returns that instance if it exists. Otherwise, VariableResolver creates a new bean (SampleView), performs the JSF wiring on any managed properties on top of the already wired spring bean, stores the bean in the scope specified, and returns it.

Above is one of the ways of integrating JSF and Spring. With this approach advantages of both the frameworks can be achieved. Using this approach one can also integrate Faces Portlets with Spring based business tier.

No comments:

Post a Comment