4

Flash, which is a temporary storage map for one request.

I am wondering how this is implemented on the grails core framework

In particular I'm interested in the class(es) responsible for putting the flash map in the request and then taking it out once the request has finessed processing.

Anthony
  • 33,838
  • 42
  • 169
  • 278

2 Answers2

10

Flash is actually a temporary storage map for the present request and the next request only. It won't retain the entries after the next request unless entries are repopulated in the next request (which would be current in future). Here is how it works in Grails:

  • FlashScope interface which extends Map itself has two methods next() and getNow() is implemented by GrailsFlashScope. All of which can be found in grails-web-mvc.

  • GrailsFlasScope mainly maintains two concurrent HasMap (one for current request and second for the next request) to hold the entries. It implements next() from FlashScope to do the cleaning and "restricting-to-next-request-only" part as:

    a. clear current
    b. make next as current
    c. clear next

  • Next thing to focus will be GrailsWebRequestFilter (implements OncePerRequestFilter) which makes sure that there is always a single execution of request per dispatch.

  • All http servlet requests are filtered by GrailsWebRequestFilter. This filter sets the flash scope to next so that every time the latest and valid info is retrieved.

  • Now the question is how does FlashScope reconciles current and next map? Well, that is why FlashScope is extended from Map. FlashScope overrides get(key) from map to reconcile both the maps by making sure values are retrieved from next map otherwise switch to current map.

  • How is flash available to controllers by default? All controller inherit ControllersApi which inherits CommonWebApi.

I hope you get what you were looking for..

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
1

If you print the class of the object:

class MyController {
  def index() {
    println flash.getClass().name
  }
}

You will see that's org.codehaus.groovy.grails.web.servlet.GrailsFlashScope. If you look at the code, there are two ConcurrentHashMap's: one for the current request and another to the next request.

To make it available, the instance is stored in the session (see registerWithSessionIfNecessary).