Spring Web Flow Bean Scopes and JSF

Ben Hale

I've recently finished up an interesting issue in Spring Web Flow. This issue (SWF-163) dealt with adding Spring 2.0 bean scoping support for Spring Web Flow's internal scopes. The implementation isn't really that interesting (the Scope interface is pretty easy to implement after all), but I wanted to mention exactly how you would use something like this in your application.

Spring 2.0 Scoping

In Spring 1.x, we had the idea of singleton and prototype bean scopes, but the notation was fixed and not especially descriptive with singleton="[true | false]". So in Spring 2.0, this notation was removed from the XSD style of configuration and now you see a notation that is more clear with scope="[singleton | prototype | ...]". Spring itself adds three more bean scopes; request, session, and globalSession which are related to web applications.

With the latest snapshots of Spring Web Flow 1.1, we now see bean scopes for the three major Web Flow scopes, flash, flow, and conversation.

<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flash"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flow"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>

To utilize these bean scopes you'll need to leverage the a new 1.1 version of the configuration (included in the Web Flow jar) and add a single element to your bean definition.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:flow="http://www.springframework.org/schema/webflow-config"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/webflow-config
            http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.1.xsd"
>

        <flow:enable-scopes/>

        <bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>

</beans>

The <enable-scopes/> tag only needs to exist once in a given application context and will allow you to use any of the three scopes contributed by Spring Web Flow.

Scoped Beans and JSF

Right now the most compelling use of these new scopes is in JSF. By using the scope notation in a standard Spring bean definition file instead of the <var/> notation in a flow definition, the experience for JSF users is much closer to standard JSF behavior. To add this ability to JSF you register the standard Spring JSF DelegatingVariableResolver. The other definitions (FlowNavigationHandler, and FlowPhaseListener) are standard for using JSF with Spring.

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
    <application>
        <navigation-handler>
            org.springframework.webflow.executor.jsf.FlowNavigationHandler
        </navigation-handler>
        <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>
    </application>

    <lifecycle>
        <phase-listener>
            org.springframework.webflow.executor.jsf.FlowPhaseListener
        </phase-listener>
    </lifecycle>
</faces-config>

The major change from the previous Web Flow enabled configuration is that now the variable resolver is the one from Spring and not Spring Web Flow. When a JSP page looks for a sale variable, JSF will delegate to Spring for bean resolution and the bean instance will be scoped according to the scope attribute on its definition.

Conclusion

The end result of this is that now JSF users can now use a syntax similar to the built-in style but in a container that is much more powerful.

If you'd like to use this new functionality, it will be coming shortly with the Spring Web Flow 1.1-m1 release, or you can get a preview by downloading the latest Spring Web Flow 1.1-m1 nightly snapshot.

Similar Posts

Share this Post
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Slashdot
  • Technorati
  • TwitThis
 

6 responses


  1. Hi Ben,

    I am trying to utilize various scopes discussed in the article.

    Let me explain what all things I have done.

    I have defined a bean called login in the flash scope as

    I have created a jsp file where the bean is referred as

    When I submit this page, in the action bean I do not get the login bean in the Flash scope. It comes as null. However if I change the scope of the login bean to flow, I get the instance. Is there any thing I am doing wrong. Would appreciate your Help.

    Regards,
    Shashi


  2. The code part was formatted hence submitting the post once more.

    Hi Ben,

    I am trying to utilize various scopes discussed in the article.

    Let me explain what all things I have done.

    I have defined a bean called login in the flash scope as
    ""

    I have created a jsp file where the bean is referred as

    "
    "

    When I submit this page, in the action bean I do not get the login bean in the Flash scope. It comes as null. However if I change the scope of the login bean to flow, I get the instance. Is there any thing I am doing wrong. Would appreciate your Help.

    Regards,
    Shashi


  3. Hi Ben,

    I am not able to format the coding lines correct. Can you please have a look at my post at http://forum.springframework.org/showthread.php?t=38731

    and http://forum.springframework.org/showthread.php?t=38781

    I will appreciate your help in this regard.

    Regards,
    Shashi


  4. Hi…
    Can you tell me the difference between the request and flash scopes.I don't know when to use one or another for doing any task…
    thanksss….


  5. [quote comment="24568"]Hi…
    Can you tell me the difference between the request and flash scopes.I don't know when to use one or another for doing any task…
    thanksss….[/quote]

    Sure! I think the documentation spells it out nicely (http://static.springframework.org/spring-webflow/docs/current/reference/flow-execution.html#execution-scopes) but I'll give you some examples as well.

    Request scope is basically the same as the request scope for a servlet. Any data stored in request scope lives for a single request. Therefore, request scoped data will not be accessible if the user refreshes a page because the refresh is a separate request.

    Flash scope aims to solve that particular problem. Any data in flash scope lives until an 'event' is signaled to the flow. Therefore, if the user refreshes a page (potentially even multiple times) the data will be accessible across each of those requests. However, as soon as the user signals an event (a submit button for instance), the data becomes inaccessible.


  6. Hi,

    I am trying to integrate SWF with JSF. The problem I am running into is trying to access the JSF managed beans inside the SWF webflow.xml. How do i access the JSF managed bean inside the Spring webflow.xml ?

    I did come across this patch but is that needed ?
    http://forum.springframework.org/showthread.php?t=37781

    TIA,
    Vijay

One trackback

Leave a Reply