Monday, June 29, 2009

JSF and Spring integration using DelegatingVariableResolver.

                          One of the design goals of Spring is to easily integrate with other Java EE-based web frameworks and technologies. In fact, integrating Spring into JSF is simply a matter of configuring the Spring DelegatingVariableResolver in faces-config.xml, the file that lists the bean resources and navigation rules for a JSF application. The Spring DelegatingVariableResolver enables a JSF application to register a custom replacement class for the standard JSF VariableResolver implementation. The standard JSF VariableResolver resolves variable references at evaluation time. The Spring DelegatingVariableResolver first delegates to the original resolver of the underlying JSF implementation, then to the Spring root WebApplicationContext. The WebApplicationContext is responsible for instantiating, configuring, and maintaining the lifecycle of a set of beans known as Spring beans. What this means is that you can configure Spring beans as managed properties of your JSF managed beans.

                  Here, for example, is a snippet of the faces-config.xml file for the sample application.
                 In this example, the Spring bean named itemSearchService is configured as a managed property of the JSF managed bean class ItemSearchHandler. A JSF managed bean with session scope is stored in the session. This means that the bean's properties stay alive for the life of the HTTP session.
                 The itemSearchService is defined as a Spring bean, ItemSearchService, in the spring configuration resource file WEB-INF/applicationContext.xml. Here is a snippet of that file,

                The element itemSearchService refers to the setItemSearchService() method in ItemController. In response, the Spring root WebApplicationContext injects the catalogService Spring bean into the catalogService property of the JSF managed bean ItemController. Here is a snippet of the ItemController source file:
                   The searchItems() method wraps a List of item objects returned from the itemSearchService in a DataModel. UIData supports data binding to a collection of data objects represented by a DataModel instance. The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a row index. The APIs provide mechanisms to position to a specified row index, and to retrieve an object that represents the data that corresponds to the current row index.
                  Final configuration for the web.xml is as follows.
                       Thats all about the configuration of Spring and JSF using DelegatingVariableResolver .

No comments:

Post a Comment