Spark Java "Flash" Scope

One of the handiest features of Grails is the “flash” scope. The flash scope is “A temporary storage map that stores objects within the session for the next request and the next request only, automatically clearing out the objects held there after the next request completes.” It’s a handy way to send messages and errors around when dealing with form posts and redirects. Since I’ve been working with Spark Java I’ve found myself missing this little feature so I decided to throw together my own implementation. I’ve added the following closure inside of my main() method in my Bootstrap class:
def flash = { request, key=null, value=null ->
if( !request.session().attribute('flash') ) request.session().attribute('flash', [:])
if( !key && !value ) {
return request.session().attribute('flash')
}
if( key && value ) {
request.session().attribute('flash')[key] = [requests: 1, value: value]
}
return request.session().attribute('flash')[key]?.value
}To manage the lifetime of the scope, I’ve added the following in my after() filter. It limits the lifetime of any flash scoped variable to 2 requests:
flash(req).each{
it.value.requests++
if( it.value.requests > 2 ) {
flash(req).remove(it.key)
}
}To read from the scope:
flash(request, 'key')
To write to it:
flash(request, 'key', 'value')
For convenience the write method also returns the value meaning you can do things like this:
def model = [
post: post,
tags: tags,
tagIds: tagIds,
error: flash(req, 'error', Messages.message('default.errors')),
]To return then entire flash scope:\
flash(request)\
Image by Fotoworkshop4You from Pixabay