In Grails the flash storage object is used to hold onto cross request data like messages.
I know that it can be accessed from most Views and Controllers, but I’m not sure if it’s available universally across Grails, or if it can only be accessed from certain conventional objects.
Can the flash object be accessed from Services, for instance?
Or even anywhere during a live web request?
What are it’s precise limitations in terms of access?
You can gain access to the
flashwherever, and more importantly, whenever you have access to a web request. In general, you can get theflashfrom theGrailsWebRequestobject.If you invoke
retrieveGrailsWebRequest()outside the context of a web request, you’ll get anIllegalStateException. TheGrailsWebRequestis bound to the current thread by a filter,GrailsWebRequestFilter, which is executed early in the service request. So basically, as long as you’re in the context of a request and “inside” this filter execution, you should be able to access the flash.Beyond that, take a look at the source for
org.codehaus.groovy.grails.web.servlet.DefaultGrailsApplicationAttributes. The flash storage is saved in the session, so theoretically you should be able to use it once you gain access to the session. Be careful though, since it’s shared among the different request for the session. The mentioned filter is responsible for advancing the state of the flash throughout requests, essentially popping aConcurrentHashMapfrom a 2-element queue.