Two J2EE technologies are becoming dominant in the Java environment: Spring and Java ServerFaces.

Spring beans could be easily integrated in a JSF project. Spring Beans can coexists with JSF managed beans or interact directly with the view.

The steps necessary to use Spring in a JSF projects are the following:

Libraries

Spring libraries must be accessible 🙂

web.xml

In web.xml you have to declare your spring listener:

<listener>
<listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

To use the request values between the view (jsp/jsf) ad the bean you have to add a second listener:

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

faces-config.xml

In faces-config.xml usually you define the managed beans. To use spring beans you have to add the following lines:

<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver><code>
</application>

applicationContext.xml

In the applicationContext.xml you have to declare your beans like a standard Spring application, the only difference is the property scope:

<bean name ="personMB" class="ch.genidea.ofac.web.jsf.Person" <strong>scope="request"</strong>>
<property name= ... />
</bean>

Usually the scope property is set as singleton (default). For the web application you have 3 possibilities:

page.jsp

In your page you can call the bean directly :

<h:outputText value="First name"/> <h:inputText value="#{personMB.firstName}" id="personName"/>
<h:outputText value="Family name"/> <h:inputText value="#{personMB.lastName}" id="personFamilyName"/>