Integrating JSF and Spring

Reducing Spring to a simple definition, we may say that it is a framework based on inversion of control (IoC is an abstract notion describing the possibility of inversion of the flow of control in comparison to procedural programming), which does not impose any specific programming model and it has become popular in the Java community as an alternative to or addition to the EJB model. In this recipe you will see how to integrate Spring with JSF.

Getting ready

Spring libraries are available at http://www.springsource.org/ and NetBeans 6.8 also comes with Spring 2.5 libraries. Installation and configuration details are beyond our scope, and they can be found at the same address.

How to do it...

Well, before mixing JSF and Spring, we should say that, from the Spring perspective, beans (also known as Spring beans) are just simple Java classes. Behind the scenes Spring beans can be declared in XML descriptors and they are exposed to client applications and managed by IoC, which means that Spring beans relationships are not manually woven. An example of a Spring bean can be seen as follows:

<bean id="carBean" class="com.spring.beans.Car">
<property name = "color">
<value>red</value>
</property>
</bean>

The preceding code snippet defines Spring beans of type Car, identified by carBean id, with a color property initialized with text red.

Now, keeping in mind this stuff, let's turn to JSF. Just to have a complete image, we should say that JSF has a correspondent of Spring beans, which are managed beans or backing beans. They are associated with UI components and they are responsible for accomplishing a web application's actions. The following code is trivial to every JSF developer, but here it is a managed bean declared in a faces-config.xml descriptor:

<managed-bean>
<managed-bean-name>paymentBean</managed-bean-name>
<managed-bean-class>
com.jsf.beans.PaymentBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now, we know that Spring uses Spring beans and JSF uses managed beans. What we may not know is that Spring provides support in such a way that a Spring bean is made visible to the JSF environment, as in the following example, where we have used the Spring carBean in JSF:

<managed-bean>
<managed-bean-name>carBean</managed-bean-name>
<managed-bean-class>
com.spring.beans.Car
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now, we have access to a Spring Bean from JSF, therefore we may say that we have mixed Spring and JSF.

How it works...

The key is the fact that the Spring bean configured in the XML files can be directly referenced in the faces-config.xml file as if it was a JSF managed Bean. Therefore it is a good technique to use Spring and JSF together for our web application.

There's more...

In the Spring community there is a project named Spring Web Flow. This project focuses on providing the infrastructure required for building and running rich web applications and also provides support for JSF. For more details check: http://www.springsource.org/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset