Integrating JSF and Spring

We’re currently using JSF and Spring together and I could say that integration of these two frameworks is not so hard thanks to the Springs facilities. I am using two different ways when I need to access the beans managed by spring. One is to use the delegating resolver and the other is to the FacesContextUtils

1) Delegating Resolver
For example you have a facade called FacadeService and it has a dependency to another service called “SomeService” which implements “ISomeService”. Also “SomeService” is managed by spring. The first is to define the FacadeService as;
package yourpackagename;
public class FacadeService {
private ISomeService someService;
public ISomeService getSomeService() {
return someService;
}
public void setSomeService(ISomeService someService) {
this.someService = someService;
}
}
The next step is to define this FacadeService as a managed bean in the faces-config.xml by; The other bean definition is a backing bean of your jsf page.
>facadeService
<managed-bean-class>
yourpackagename.FacadeService
</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
<managed-property>
<property-name>someService</property-name>
<property-class>
packagename.ISomeService
</property-class>
<value>#{someService}</value>
</managed-property>
</managed-bean>

>backingBeanName
<managed-bean-class>
yourpackagename.backingBeanClass
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>facadeService</property-name>
<property-class>
packagename.FacadeService
</property-class>
<value>#{facadeService}</value>
</managed-property>
</managed-bean>
* Also the delegating resolver must be defined in faces-config.xml between the application tag as following;
<application>
<
variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>;
<
/application>
* Next step is putting the listener of spring goes into the web.xml and telling the location of spring’s xml
<context-param>
<param-name>contextConfigLocation
/WEB-INF/applicationContext.xml
The place of applicationContext.xml is optional, you should also put it under classes and refer it like classpath:/applicationContext.xml.


org.springframework.web.context.ContextLoaderListener

* Here is the spring’s xml to manage the someService bean


“-//SPRING//DTD BEAN//EN”
“http://www.springframework.org/dtd/spring-beans.dtd”>
default-dependency-check=”none”>

* So thats all metadata you need to configure, the final step is to add the FacadeService to the code behind of your jsf page by simply adding this; For example you have a backing bean called YourBackingBean.java, Faces inject facadeService to this bean since we have configured it. Also the spring bean (some service) is injected via the delegation variable resolver when creating the facadeService.
public class YourBackingBean{
protected FacadeService facadeService;
public FacadeService getFacadeService() {

return facadeService;
}
public void setFacadeService(FacadeService facadeService) {
this.facadeService = facadeService;
}
}
There is an important note here, using the #{facadeService} you can get the value of the FacadeService managed by Faces, the default variable resolver of JSF will bring it for you. Also you can get the SomeService object by writing the appropriate accessor methods and replacing the value binding expression with #{someService}. The default variable resolver will not find “someService” and the responsibility will pass to the DelegatingVariableResolver of Spring which will return the someService name. Long story short, here comes the fun part, accessing the spring bean from code behind, for example at anywhere in your jsf code behind you can reach the spring bean by;
getFacadeService().getSomeService().someMethod(); //If everything is ok, hopefully no npe
These steps allows the JSF-Spring integration, inversion of control is applied both by spring and faces, facadeservice is managed by Faces and these are wired using the delegating variable resolver of spring. Important thing is spring bean is not injected by spring, the wiring is done by faces. Spring’s role is a container in this case.
2) Using the FacesContextUtils
I always see this one as the alternative and I dont use it unless I have to. For example, I have created a custom jsf component and a custom variable resolver. Both of these needed to use spring beans. So thats why I’ve reached spring directly. FacesContextUtils is an utility class that is located in spring framework.
ISomeService someService = (ISomeService) FacesContextUtils
.getWebApplicationContext(FacesContext.getCurrentInstance())
.getBean(“someService”);
someService.someMethod();
To sum up, I could say using these two frameworks is fun and spring’s mechanisms make it very easy.You can either use the delegating resolver or FacesContextUtils to use the beans managed by Spring and Faces together.

0 comments: