recursive.codes


The Personal Blog of Todd Sharp

Spark Java "Flash" Scope

Posted By: Todd Sharp on 4/18/2017 3:33 GMT
Tagged: Groovy, Java, Spark Java

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:

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:  

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:

To return then entire flash scope:

flash(request)

Image by Fotoworkshop4You from Pixabay



Related Posts

Running Database Queries With REST via a Database Tools Connection (IE: The Super Easy Way)

Running Database Queries With REST via a Database Tools Connection (IE: The Super Easy Way)

I recently blogged about a brand-new service in the Oracle Cloud - Database Tools. It's a safe, secure way to store your database credentials in the...

Easy and Secure Oracle Database Connections With Database Tools

Easy and Secure Oracle Database Connections With Database Tools

There's a brand new tool in town, and it's ready to make your life a whole lot easier if you work with Oracle DB in the cloud. It's called "Database Tools...

Quick Pub/Sub Testing for Oracle Advanced Queuing With a Command Line Interface (CLI)

Quick Pub/Sub Testing for Oracle Advanced Queuing With a Command Line Interface (CLI)

I have been spending a lot of time playing around with Oracle Advanced Queuing (AQ) lately and one of the things that has bothered me is the lack of a ...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!