recursive.codes

recursive.codes

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

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Sending Email With OCI Email Delivery From Micronaut

Sending Email With OCI Email Delivery From Micronaut

Email delivery is a critical function of most web applications in the world today. I've managed an email server in the past - and trust me - it's not fun...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

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!