5 Flow Scopes - Reference Documentation
Authors: Graeme Rocher
Version: 2.1.0
5 Flow Scopes
Scope Basics
You'll notice from previous examples that we used a special object calledflow
to store objects within "flow scope". Grails flows have five different scopes you can utilize:
request
- Stores an object for the scope of the current requestflash
- Stores the object for the current and next request onlyflow
- Stores objects for the scope of the flow, removing them when the flow reaches an end stateconversation
- Stores objects for the scope of the conversation including the root flow and nested subflowssession
- Stores objects in the user's session
Grails service classes can be automatically scoped to a web flow scope. See the documentation on guide:services for more information.Returning a model Map from an action will automatically result in the model being placed in flow scope. For example, using a transition action, you can place objects within
flow
scope as follows:enterPersonalDetails { on("submit") { [person: new Person(params)] }.to "enterShipping" on("return").to "showCart" }
- Moves objects from flash scope to request scope upon transition between states;
- Merges objects from the flow and conversation scopes into the view model before rendering (so you shouldn't include a scope prefix when referencing these objects within a view, e.g. GSP pages).
Flow Scopes and Serialization
When placing objects inflash
, flow
or conversation
scope they must implement java.io.Serializable
or an exception will be thrown. This has an impact on guide:GORM in that domain classes are typically placed within a scope so that they can be rendered in a view. For example consider the following domain class:class Book {
String title
}
Book
class in a flow scope you will need to modify it as follows:class Book implements Serializable { String title }
class Book implements Serializable { String title Author author }
Author
association is not Serializable
you will also get an error. This also impacts closures used in guide:eventsAutoTimestamping such as onLoad
, onSave
and so on. The following domain class will cause an error if an instance is placed in a flow scope:class Book implements Serializable { String title def onLoad = { println "I'm loading" } }
onLoad
event cannot be serialized. To get around this you should declare all events as transient
:class Book implements Serializable { String title transient onLoad = { println "I'm loading" } }
class Book implements Serializable { String title def onLoad() { println "I'm loading" } }
The flow scope contains a reference to the Hibernate session. As a result, any object loaded into the session through a GORM query will also be in the flow and will need to implement Serializable.If you don't want your domain class to be Serializable or stored in the flow, then you will need to evict the entity manually before the end of the state:flow.persistenceContext.evict(it)