Chariot Training Classes

Training Courses

I run Chariot's training and mentoring services. We provide training in AngularJS, HTML5, Spring, Hibernate, Maven, Scala, and more.

Chariot Education Services

Technology

Chariot Emerging Tech

Learn about upcoming technologies and trends from my colleagues at Chariot Solutions.

Resources

Chariot Conferences

Podcasts

« Setting up the IntelliJ Plugin for Groovy and Grails | Main | New Chariot Tech Cast episode : Rails 101 »
Friday
Apr112008

Grails and Spring WebFlow

Everyone has had to code an application at some point where they were forced into a particular set of navigational flows. There are a few ui-centric workflow packages out there, including Open Symphony's OSWorkflow, and Spring's WebFlow. Other developers at my shop have worked with WebFlow and were pleased with its' features. But did you know that Grails embeds WebFlow and makes it available within its' controllers automatically?

WebFlow Basics

For a thorough introduction to Spring Webflow, I suggest visiting the SpringSource site. However, in general, webflows can be broken up into distinct components:


  • State - This is a 'definable moment' within the webflow, such as a View State, where the application is waiting on user input.

  • Transition - An event, often fired by a user taking an action, that moves the webflow from one state to another.
  • Action - Code that can be performed within a transition, or on the start or ending of a given State

  • View - A (GSP) page that is rendered during a 'view state'.
  • Flow Scope - A semi-session-like container that lives for the life of the webflow. Data captured from one view to the next

Without too much more ceremony, I can simply say that although Spring WebFlows are a great feature, and take a lot of pain out of implementing flow-based application logic, they are made even easier by Grails.

A Grails WebFlow

Here is a simple webflow that represents a fragment of a voting application. Call it KrimpleVote (or HangingChad, the system)... (I have since lost the image itself...) To build a webflow in Grails, simply create a closure ending in Flow, and embed the states, transitions and actions as inner closures. It's easier to read than it is to describe:



def registrationFlow = {

showDisclaimer {
on("continue").to "lookupVoter"
}

lookupVoter {
on("selfRegister").to "selfRegister"
on("find") {
if (!params.ssn) {
no()
return error()
}

def voter = Voter.findBySsn(params.ssn)
if (voter == null || voter.ssn == null) {
no()
return error()
} else {
flow.voter = voter
}
}.to "showRegistration"
on("return").to "lookupVoter"
}

selfRegister {
on("continue") {
Voter v = new Voter(params)
if (!v.validate()) return error()
v.save()
flow.voter = v
}.to "chooseParty"
}
...

View states are closures that do not automatically transition to another state. Take a look at the showDisclaimer closure as it's a view state. It waits for the 'continue' event to be sent as a transition from the user. You do that by submitting a form to the same closure (registration) and embedding a submitButton within the form with the name of the transition:








fragment of showDisclaimer.gsp

Note: You have to put the GSPs in a subdirectory named registration under the controller's views as it puts all webflow views in a subdirectory by flow name (without Flow). Also, the pages are pretty straightforward as well. Another nice thing is that it automatically handles the flow continuation key for you.

I've been (slowly) getting around to putting that Voter application together as a sample. Once I do, I will post it for download. Until then, I'll post some snippets as I learn more.

For more Flow goodness, check out the grails documentation page on it.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Everyone has had to code an application at some point where they were forced into a particular set of navigational flows. There are a few ui-centric workflow packages out there, including Open Symphony's OSWorkflow, and Spring's WebFlow. Other developers at my shop have worked with WebFlow and were pleased with its' ...

Reader Comments (5)

Thanks for this great article, since I am planning to start looking into Webflow, this will be good start.

Thanks

Sen

April 12, 2008 | Unregistered CommenterSen

[...] Spring WF 1 with Grails [...]

Great article, adding it to my favourites!

February 28, 2009 | Unregistered Commenterwii charts

Just read some other comments on your blog, and I agree with the general impression, your doing a great job!Keep it up!

Your writing style is quite a god guide for me if you like - I have recently started my own blog and I am really struggling to write articles!

March 6, 2009 | Unregistered Commentercondo insurance

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>