JavaServer Faces Integration


Special thanks to Arjen Poutsma for preparing content for this section.

Both Spring and JSF have their own specific format to define beans: Spring uses <bean>; JSF uses <managed-bean> blocks. Although the format is somewhat different, the concepts underlying both are quite similar. This can be seen by looking at a typical JSF-managed bean definition:

 <managed-bean>   <managed-bean-name>myJsfManagedBean</managed-bean-name>   <managed-bean-class>example.MyJsfManagedBean</managed-bean-class>   <managed-bean-scope>session</managed-bean-scope> </managed-bean> 

This block defines a JSF-managed bean named myJsfManagedBean, which is of class example.MyJsfManagedBean. Because the scope of this bean is defined as session, only one instance of the JSF-managed bean will be created per HTTP session.

Integrating Spring with JSF centers around the concept of bean resolution, meaning that you can refer to Spring-managed beans within a JSF-managed bean or a JSF page. This is done using the Delegating VariableResolver, provided by Spring. This is a standard JSF VariableResolver that delegates to the original JSF resolver first, and then to the Spring root WebApplicationContext (loaded by the org.springframework.web.context.ContextLoaderListener). Spring also provides the org.springframework.web.jsf.FacesContextUtils with which you can get a reference to the Spring WebApplicationContext yourself. This class resembles the WebApplicationContextUtils a lot and a getWebApplicationContext() and a getRequiredWebApplicationContext() method, but using a FacesContext to look up the Spring context.

To use the Spring VariableResolver, put the following in your faces-context.xml:

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

Let's say you have a Spring-managed bean in your applicationContext.xml:

 <bean       >   <!-- additional details omitted --> </bean> 

You can now refer to this bean in your JSF-managed beans and pages using the standard JSF expression #{mySpringManagedBusinessObject}. The #{} notation is specified by JSF and resembles the ${} notation used in JSP a lot. The difference here is that when using the JSF notation, the bean will be looked up in your FacesContext, whereas using the JSP notation, the PageContext will be inspected. The expression language principles used in JSTL and JSP 2.0 apply to JSF expressions as well, by the way, so you can use the #{mySpringManagedBusinessObject.users} expression to retrieve a collection of users from a getUsers() method. The following is an example of how you can refer to the Spring-managed bean from within your JSF configuration:

<managed-bean>    <managed-bean-name>myJsfManagedBean</managed-bean-name>    <managed-bean-class>example.MyJsfManagedBean</managed-bean-class>    <managed-bean-scope>session</managed-bean-scope>    <managed-property>      <property-name>mySpringManagedBusinessObject</property-name>      <value>#{mySpringManagedBusinessObject}</value>    </managed-property> </managed-bean>

Note that it's also possible to refer to your Spring-managed bean from within a JSF page:

 <h:inputText value="#{myJsfManagedBean.mySpringManagedBusinessObject.name}" .../> 

Note that there is a SourceForge project (jsf-spring) that makes it possible to refer to JSF-managed beans within your Spring context and to configure Spring beans in a JSF configuration file. This project also provides functionality to scope Spring beans in the same way you can scope JSF beans, giving you the additional session and request scopes. This project provides more advanced and tighter integration with JSF. You can find out more about it at http://jsf-spring.sourceforge.net.



Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net