3 Action States and View States - Reference Documentation
Authors: Graeme Rocher
Version: 2.1.0
3 Action States and View States
View states
A view state is a one that doesn't define anaction or a redirect. So for example this is a view state:enterPersonalDetails {
on("submit").to "enterShipping"
on("return").to "showCart"
}grails-app/views/book/shoppingCart/enterPersonalDetails.gsp by default. Note that the enterPersonalDetails state defines two events: submit and return. The view is responsible for triggering these events. Use the render method to change the view to be rendered:enterPersonalDetails {
render(view: "enterDetailsView")
on("submit").to "enterShipping"
on("return").to "showCart"
}grails-app/views/book/shoppingCart/enterDetailsView.gsp. Start the view parameter with a / to use a shared view:enterPersonalDetails {
render(view: "/shared/enterDetailsView")
on("submit").to "enterShipping"
on("return").to "showCart"
}grails-app/views/shared/enterDetailsView.gspAction States
An action state is a state that executes code but does not render a view. The result of the action is used to dictate flow transition. To create an action state you define an action to to be executed. This is done by calling theaction method and passing it a block of code to be executed:listBooks {
action {
[bookList: Book.list()]
}
on("success").to "showCatalogue"
on(Exception).to "handleError"
}success event will be triggered. In this case since we return a Map, which is regarded as the "model" and is automatically placed in flow scope.In addition, in the above example we also use an exception handler to deal with errors on the line:on(Exception).to "handleError"handleError in the case of an exception.You can write more complex actions that interact with the flow request context:processPurchaseOrder {
action {
def a = flow.address
def p = flow.person
def pd = flow.paymentDetails
def cartItems = flow.cartItems
flow.clear() def o = new Order(person: p, shippingAddress: a, paymentDetails: pd)
o.invoiceNumber = new Random().nextInt(9999999)
for (item in cartItems) { o.addToItems item }
o.save()
[order: o]
}
on("error").to "confirmPurchase"
on(Exception).to "confirmPurchase"
on("success").to "displayInvoice"
}Order object. It then returns the order as the model. The important thing to note here is the interaction with the request context and "flow scope".Transition Actions
Another form of action is what is known as a transition action. A transition action is executed directly prior to state transition once an event has been triggered. A simple example of a transition action can be seen below:enterPersonalDetails {
on("submit") {
log.trace "Going to enter shipping"
}.to "enterShipping"
on("return").to "showCart"
}submit event that simply logs the transition. Transition states are very useful for data binding and validation, which is covered in a later section.