Access WebFlow Scope From a Command Class

One of many nice things in Spring that I like is that you can get access to the current request context in any class you like, without passing different parameters around (which may not be possible in some cases anyway). The “magic” is in the RequestContextHolder class (org.springframework.web.context.request.RequestContextHolder) that bounds treads to request attributes.

If you need to access WebFlow scope situation is equally simple. Here’s how you can get WebFlow scope in Command class validator (pay attention on RequestContentHolder class that comes from a webflow package tree):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.webflow.execution.RequestContextHolder as WFRCH

public class IssueFormCommand implements Serializable {
    String dummyField
    ...
    static constraints = {
        dummyField(validator: {val, obj, errors ->
            def rc = WFRCH.getRequestContext()
            def flowScope = rc.getFlowScope()
            def flowParams = rc.getRequestParameters()
            // do something with it...
            // e.g. use parameters from the flow scope to validate submitted form
        })
    }
}

(The example above is of course in Grails/Groovy.)

In the same manner you can access the current WebFlow scope from other classes in your execution context.

Comments