HowTo: Spring webflow in grails

with No Comments

Problem:

Need to implement into grails project spring web flow

Solution:

I created an easy grails project. You can download it from github.

I’m working with grails 2.3.11, so I choose older webflow plugin:
compile ':webflow:2.0.8.1'

After it you can create controller and add to it flow with one step:

Let’s speak about what I did:

First of all you need marked action how flow: purchaseFlow

You can do it add to end of action “Flow” during compilation it signal that it’s will be flow and need to generate config for it (if it will be interest to someone I will write about how it is work).

Next

init {}

Flow will automaticaly go to first closure after redirect to it. So I named it init in it we can init variables that we need

flow.purchasedProduct

Flow variable has flow scope i.e. while you are in flow you can use it. Web flos use configurated HTTP session, so flow it’s upgraded session but you don’t need to worry about clearing it and some more features. To put objec to flow and remove some bugs all you classes should be Serializable, so do it right now.

For implementing logic you must di it in:
action {} closure

It means that in web flow you have 2 types of actions: logics that covered action{} and rendering, that just render view. In last one we cannot user flow variable. All params that you want using on view should put to flow. It’s how usually put it to render model.

on("success").to "productCountSelect"
on(Exception).to "exit"

‘on’ method take two parameter – status and closure(about it later). There are few predefined, but I usually use only two. There are “success” that indicate that all ok and Class(here it’s Exception) it’s indicate that error throw. With Class params, you cannot add closure – it’s not so flexible how string status.

And for the last I want to describe about statuses that you predefined!! All statuses must be predefined because implementing of web flow. If you read spring docs you see that in Java you should prenamed all steps in xml.

Grails do it instead of you. But all should be predefind.

So, your own status:

on("fail"){flash.error = “Fail”}.to "enterAddress"

I name it “fail”. To call it you should return it :return fail()
also in all statuses you can do some logic before you go to next step:

on("fail"){flash.error = “Fail”}

So let’s finished our flow!

Last version

Leave a Reply