tags:

views:

939

answers:

1
+1  Q: 

Grails flash scope

Hi,

In my Grails app I have a controller action that does this:

def activeMember = {ConfirmSignUpCommand signupCommand ->

    flash.signupCommand = signupCommand
    render(view: "confirmPassword")
}

When the form on confirmPassword.gsp is submitted it is handled by the following action:

def validatePasswordConfirmation = {

    def password = params.password
    def command = flash.signupCommand
}

However, when I reach the validatePasswordConfirmation action the flash scope is empty. I'm 100% certain that there are no requests submitted between these two controller actions. Where has my flash-scoped object gone?

+2  A: 

The flash object would be available to the next request, in your scenario that is the rendering of the confirmPassword.gsp page (many times it's also used to pass objects to another controller via a redirect). When the confirmPassword.gsp page submits that is another request and the object you placed in flash is cleared automatically.

John Wagenleitner
I thought that processing the `activeMember` action and rendering the `confirmPassword` view are both part of the same request. Guess I was wrong. I don't see why I'd use flash scope for passing data from an action to the view when I can just use the model. So I guess flash scope is only really useful when passing data following a redirect?
Don
I see flash used frequently for passing messages or message codes to the view (i.e., Event x has been saved). That seems to be a common use case for flash along with stashing objects for a forward/redirect.
John Wagenleitner
Can you think of any particular reason why you might use the flash scope for passing messages to the view, rather than the model (or request scope)? I can't......
Don
I think the flash is more appropriate for messages about the actions a controller has taken (saved, updated, deleted, etc.). That would out of place in the model in my opinion. You can see flash used for messaging in the grails generated controllers/views.
John Wagenleitner