JSF-Spring integration is very straightforward and simple to do.
Step 1 – Changes in Web.xml
Your Web Application need to be aware of Spring's Web Application Context. This is defined in the Web.xml.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Your Web Application also needs to know the location of Spring Beans. Definition of one or more "contextConfigLocation" does exactly this job.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml </param-value>
</context-param>
The above statement makes it possible to load classless from applicationContext.xml, which is in the classpath.
Below is an example of applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="destinationService" class="com.cyberworkz.leisure.services.DestinationServiceImpl">
</bean>
</beans>
Step 3 Changes in the FacesConfig.xml
Variable names in JSF are resolved by "Resolvers". For every "bean" that is declared in the faces-config.xml, JSF resolver will try to construct and associate the object with the identifier. Now that we are using Spring, we have to let JSF Framework know that instead of the default JSF resolver, Spring resolver should be used. Spring's distribution comes with a pre-defined Variable resolver called org.springframework.web.jsf.DelegatingVariableResolver. The following declaration in faces-config.xml will do exactly that:
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
And that’s all we need to do.
Comments