Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does flash object map work inside grails framework

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.

like image 245
Anthony Avatar asked Dec 20 '22 16:12

Anthony


1 Answers

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

like image 54
dmahapatro Avatar answered Jan 12 '23 19:01

dmahapatro