I have a filter that is currently defined to run after the view is rendered:
class MyFilter {
def filters = {
doStuff(controller: '*', action: '*') {
before = {...}
after = {...}
afterView = {
// code I want to run when EVERYTHING is set and done }
Holder.setCurrentData(null)
}
}
}
This should work, but I noticed that <g:message /> tags (and possibly others; this is the one what interests me at this point) are executed after the afterView filter.
This is a problem because I use this filter to keep track of some information for the current execution in a ThreadLocal, and I want to make sure I clean up after myself when the request is done. I don’t want to use the request/session object to shuffle the data along, because then I have to pass it to all the calls I make; as it is, I have a Holder class that I can query for the value in the ThreadLocal.
I need the information from that ThreadLocal in my custom MessageSource. That’s how I noticed that <g:message /> is called after the afterView filter.
You can use a servlet filter:
Run
grails install-templatesif you haven’t already and then you can register it in src/templates/war/web.xml like this:I’ve extended
GenericFilterBeanfor convenience but you could also just implement thejavax.servlet.Filterinterface directly. It could also be written in Groovy, but I tend to write filters in Java since they’re called for every request, and the small overhead that Groovy adds can add up here.