Integrating JSF with Spring
Spring comes with a JSF variable resolver that lets you use JSF and Spring together. You can access Spring beans using JSF and vice versa. Integrating JSF and Spring lets you tap into the capabilities of two of the most powerful server-side Java frameworks.
How do I do that?
You already did! In the previous example you used value bindings to
bind JSF components to model objects. For example, in
/bike.jsp
, you did this:
...
<h:form>
<h:dataTable value="#{rentABike.bikes}
" var="bike">
<h:column>
...
</h:column>
</h:dataTable>
...
</h:form>
...
The #{rentABike.bikes}
value binding references
the bikes property of the bean named rentABike
.
Recall that previously, the rentABike
bean was
defined in a Spring configuration file, like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dt\
d/spring-beans.dtd">
<beans>
<bean name="rentABike
" class="com.springbook.ArrayListRentABike">
<property name="storeName"><value>Bruce's Bikes</value></property>
</bean>
...
</beans>
Things are no different in the JSF version of
Bruce's Bike Shop, which means that the
JSF expression
#{rentABike.bikes}
accesses a Spring bean. That's
made possible by the DelegatingVariableResolver
from the org.springframework.web.jsf
package. You
declare that variable resolver, which extends the JSF expression
language to include references to Spring beans, in the faces
configuration file:
<faces-config> <application> <variable-resolver> ...
Get Spring: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.