Grails flash scope

Tag: grails Author: sakuyasama Date: 2009-08-11

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?

That is wierd, are you sure the flash scope is indeed empty, it is giving an attribute not found error? signupCommand is never null in the activeMember action? If all else fails try a grails clean.

Best Answer

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.

comments:

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?
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.
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......
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.