Section 3.3. Integrating JSF with Spring

team bbl


3.3. 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.

3.3.1. 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" >         <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>          org.springframework.web.jsf.DelegatingVariableResolver       </variable-resolver>    </application>    ... </faces-config>

3.3.2. What just happened?

You saw how easy it is to integrate JSF and Spring. You can access Spring beans with the JSF expression language. That seamless integration makes it easy to use these two powerful frameworks together.

3.3.3. What about...

...JSF managed beans? Like Spring, JSF has a bean factory. You specify managed beans in a faces configuration file and JSF instantiates and initializes those beans. In our JSF version of Bruce's Bike Shop, we didn't use any managed beans; instead, we used Spring beans exclusively. However, most web applications built on JSF and Spring will have ample quantities of both Spring beans and managed beans.

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

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