Introduction
As a frequent user of the Spring MVC framework, I had the opportunity to work with another “Model 2″ framework of Spring, namely Webflow.
Working with Webflow was a great experience for me, as it showed me a different point of view regarding handling view logic, and I like to share some experiences and opinions with you.
If you want to know more about the technical realization of using Webflow in your application, I advise you to read the articles referred to at the bottom of this post.
Workings
The main goal of Webflow is to make it easier for the programmer to handle navigation through the website under development. Nowadays a programmer can get really overwhelmed with all the files that make up his flow through a website. To get a clear picture of the flow he needs to, in the case of Spring MVC, browse through several JSP files, look for the controllers mapped to URLs, and check what view those will return. A tedious process.
Spring Webflow solves this ‘problem’ by letting you define the navigation through a website using a natural, predefined, flow. The advantage of this is that you have a full, and clear, overview of some navigational paths in your application together in one (or more) files, not bothering you with other concerns. I explicitly say ‘some navigational paths’, because you simply cannot model all the paths in your application: Webflow’s primary goal is to support the flow and navigation through a business process, where the user is bound to the predefined flow. These predefined flows are the scope of Webflow. More on this later on.
In code this flow is constructed, for example, using an XML file.
<webflow id="buyArticle" start-state="obtainArticleInfoAction">
<action-state id="obtainArticleInfoAction">
<action bean="articleActions" method="bindAndValidate"/>
<transition on="success" to=" showArticlesView "/>
</action-state>
<view-state id="showArticlesView" view="articleOverview">
<transition on="submit" to="isUserLoggedInDecision"/>
</view-state>
<decision-state id="isUserLoggedInDecision">
<action bean="securityUtils" method="isUserLoggedIn"/>
<transition on="yes" to="orderItemsActions "/>
<transition on="no" to="LoginView "/>
</decision-state>
<action-state id="orderItemsActions">
<action bean="ArticleActions" method="orderArticles "/>
<transition on="success" to="ThanksForYourOrder "/>
<transition on="error" to="errorView"/>
</action-state>
<end-state id="ThanksForYourOrder" view="thankYou" />
</webflow>
As you can see, we have 4 types of states.
1. Action-state
2. Decision-state
3. View-state
4. End-state
There is one more,m but this one is not shown in the example.
5. Subflow-state
• A view state will simply show you a webpage, and waits for input. This state can be compared by a MVC ‘View’.
• In an action state you can call a method on your business layer to perform some actions; retrieve data to populate your shop, place an order or login a user. If the action completes it will redirect you to another state, like a view state, so you can show data you retrieved in the action state. This state can be compared with a MVC ‘controller’.
• Within a decision state you can validate your data, like a conditional statement (if/else, switch). Is the user already logged in? Has he reached his order limit? Depending on the outcome you can direct the user to another state.
• The end state is the final state the flow will enter. Its main purpose is to let the user know his order was processed (or not), and the flow is done. When reaching this state, all resources associated with the flow are cleaned up.
• The subflow state spawns another flow as a subflow.
To sum up: You define your flow in states, in which you can do certain actions. By chaining the states, you create a flow in which the user hops from state to state while navigating through the site.
Experiences
The biggest advantage of Webflow is getting a clear overview of the business processes and how they are implemented as a flow throughout your application. In standard MVC web applications, you were forced to perform the tedious task of navigation through a lot of files to get a clear understanding of the page flow. You had to click through your JSP, find the mapping to the controller, look what view it returns, and browse to that view (over and over again) – all these just to visualize the page flow of the application (Note: there might be a better way; however I haven’t found it yet). With Webflow you can have an oversight of the flow in one file.
One positive side effect of the clear overview of Webflow is that the sketches you used to make to visualize the flow of your application can actually be used here. The flow of the application can be drawn in an UML diagram or even a flowchart, so everybody can look at it and understand it. A business analyst can create the flowchart, where the developer, after that, can use it to code the flow definition in XML. Personally, I prefer using this flowchart principle, because it is the most commonly understood. Your developers, business analysts, even your customers have seen a flowchart once in their lives. So using it makes communicating to, and between, these groups much easier.
Another positive experience comes out of the fact that it is much easier to reuse certain parts (actions) of your project. Where in classic MVC frameworks the executed actions depend on the actual URL called and parameters passed (so a controller is tightly coupled with the view), in Webflow you can reuse a state and use it somewhere else. They are self-contained components and are not (per definition) coupled to a view.
One thing you need to keep in mind though is that Webflow will not replace your presentation framework, it cannot render the view itself, and thus needs a complementary framework that can, like struts or Spring MVC. Spring Webflow is just there to aid you in your development of your presentation logic; for a clear overview and definition of the whole page flow.
One small disadvantage I encountered using Webflow is that it is meant to be used for websites which require an outlined flow, a standard path, like ordering an article for example. These kinds of paths are often determined and controlled by a business processes and usually need a certain amount of steps, which the user needs to traverse, to finish the process. This is called a controlled navigation. Webflow is not suited for applications like online communities or blogs, because there aren’t any pre-defined paths the user needs to follow there. The user can just click on any link and visit any page he wants to. As the user is free to browse to whatever page he wants, you cannot define the flow anymore.
Note: As Webflow was not meant to support this kind of free browsing, it cannot really be seen as a true disadvantage of the product. However it would be nice if it was somehow supported, because the product really helps in creating a clear overview of your page flow.
(Dis)Advantages summary
+ A business analyst can make the flowcharts needed for the flow. He understands the business logic the best so he can model it to the views without the need of any technical skills.
+ It is complementary to other presentation frameworks like struts and Spring MVC. You are not bound to one technology to create your views.
+ The ability to reuse states as black-box components.
- It is not ideally suited for web sites that require a lot of free browsing. It is more suited for a business process, like ordering an article (controlled navigation).
Other resources
If you want to read more about the technical workings of Webflow and how to use it in you application I suggest the following pages. Some are a bit outdated, but still very handy for giving you an introduction how Webflow works.
Spring Web Flows: A Practical Guide
Complete tutorial of a simple Webflow application.
http://www.ervacon.com/products/springwebflow/article/article.html
Spring Web Flow Examined
Technical article by Steven Devijver of Interface21
http://www.javalobby.org/articles/spring-webflow/
Managing Web Conversations with Spring Web Flow
Powerpoint presentation by Keith Donald
JavaZone, 2005
http://opensource.atlassian.com/confluence/spring/download/…
Confluence homepage of the Webflow project
http://opensource.atlassian.com/confluence/spring/display/WEBFLOW/Home