How to declare a JSF managed bean in a Spring 3.1 application?
This my first experience in developing applications based on a Java EE architecture and I am using
- JSF 2.0
- Spring 3.1
- JPA 2.0
I want to ask you please about an issue which I am a little bit confused with. I want to declare my JSF managed bean but there's many solutions to deal with this constraint:
- Declare the manged bean in the faces-config.xml.
- Override my controller with @ManagedBean annotation.
- Override my controller with @Controller annotation.
My question is : what's the diffrence between these three solutions ?! and what changes should i make on my ApplicationConfig.java or/and web.xml ?! And concerning Spring , should i put some kind of listeners in my web.xml ?!
I really tried to read many tutorials and examples but each time i find myself blocked !
When integrating Spring and JSF you really can't be a beginner in either technology, because they don't play well together. My first and best advice to you is to get a few books on JSF and Spring and really understand both separately before attempting to integrate them.
With that being said, JSF is a component based web framework with an emphasis on MVC. Spring is a Dependency Injection and Inversion of Control framework that is not exclusive to web applications.
If you don't understand these three terms are:
Component based web framework
Inversion of Control
Then my suggestion is that you just stop what you are doing and immediately begin reading.
The main problem with integrating these two things is that there is some overlap in responsibilities between the two frameworks that need to be addressed. JSF as a standalone framework maintains the scope of its own managed beans without the need for a seperate DI framework. When introducing Spring however then there are naturally going to be conflicts. Spring manages its own Beans apart from JSF, so to reference these ManagedBeans and have business objects or DAO's be properly injected into them for use, the JSF ManagedBeans need to become Spring Controllers.
You can declare a JSF ManagedBean with the @Controller annotation. Spring 3 is smart enough to recognize that it is a JSF managed bean and the bean name will be whatever the name is declared as for the ManagedBean.
@Controller @Scope("session") @ManagedBean(name="testBean")
Now that this is handled, the next problem is that pesky EL Resolver that came with your JSF implementation. The EL Resolver does basically just that, it resolves EL expressions encountered on your XHTML/JSF page. When referencing testBean however it will not be able to resolve this name correctly as it is referring to a JSF managed bean by that name, and will not be able to find the Spring Controller with all the Spring injected dependencies that you need.
Spring 3 solves this problem by providing you a custom EL Resolver to use in place of the one that comes bundled with your JSF implementation. You can declare it to be used in faces-config.xml
<application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application>
should i put some kind of listeners in my web.xml ?!
If you are just integrating JSF + Spring without the need for any other Spring controlled Servlets or without the need for Spring Security integration then no you do not need anything additional in your web.xml. You would only need to declare the FacesServlet and its context params, plus any other third party component library servlets that may be necessary for your situation.