简体   繁体   中英

Grails filters: Any way of chaining filters?

Is there a way to chain several filters in a grails application (as in Java filters)? Maybe something with spring?

I've written a couple of filters, and would like to get them to execute serially (order is not particularly important). The reason behind this? I need to write about 20, 30 filters, and don't want them all in the same file.

I've read about Spring's DelegatingFilterProxy but can't figure out on how to configure it to chain all my grails filters.

Since Grails 1.3.1 you can chain filters by using the "dependsOn" keyword:

def dependsOn = [MyOtherFilters.class]

http://jira.codehaus.org/browse/GRAILS-6229

I may not be understanding the core issue here, but the simple answer might be "they're already chained". Filters are executed based on the selector you put in the filter closure (eg myPreProcessorFilter(controller:' ', action:' ') {}). All selectors that match your controller/action will execute. I do this all the time with logging and performance measurement filters.

Here's an example. Both the logAction and measureMethodTime filters will be applied to all controllers and actions (since I left the selector wide open).

    import org.springframework.web.context.request.RequestContextHolder as RCH
import com.x.y.*

class PerformanceFilters {
    def filters = {

        logAction(controller:'*', action:'*'){
            before = {
                log.debug("${controllerName}.${actionName}: entering; params=${params}")
            }
        }

        measureMethodTime(controller:'*', action:'*'){
            before = {
                def session = RCH.currentRequestAttributes().getSession(false)
                if (session)
                {
                    Q.startTimer("${session.id}-${controllerName}-${actionName}", "method.${controllerName}.${actionName}")
                }
            }

            afterView = {
                def session = RCH.currentRequestAttributes().getSession(false)
                if (session)
                {
                    Q.stopTimer("${session.id}-${controllerName}-${actionName}", "method.${controllerName}.${actionName}")
                }
            }
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM