7.6.1 Applying Filters - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 2.3.8
7.6.1 Applying Filters
To create a filter create a class that ends with the conventionFilters
in the grails-app/conf
directory. Within this class define a code block called filters
that contains the filter definitions:class ExampleFilters { def filters = { // your filters here } }
filters
block has a name and a scope. The name is the method name and the scope is defined using named arguments. For example to define a filter that applies to all controllers and all actions you can use wildcards:sampleFilter(controller:'*', action:'*') { // interceptor definitions }
- A controller and/or action name pairing with optional wildcards
- A URI, with Ant path matching syntax
controller
- controller matching pattern, by default * is replaced with .* and a regex is compiledcontrollerExclude
- controller exclusion pattern, by default * is replaced with .* and a regex is compiledaction
- action matching pattern, by default * is replaced with .* and a regex is compiledactionExclude
- action exclusion pattern, by default * is replaced with .* and a regex is compiledregex
(true
/false
) - use regex syntax (don't replace '*' with '.*')uri
- a uri to match, expressed with as Ant style path (e.g. /book/**)uriExclude
- a uri pattern to exclude, expressed with as Ant style path (e.g. /book/**)find
(true
/false
) - rule matches with partial match (seejava.util.regex.Matcher.find()
)invert
(true
/false
) - invert the rule (NOT rule)
- All controllers and actions
all(controller: '*', action: '*') {}
- Only for the
BookController
justBook(controller: 'book', action: '*') {}
- All controllers except the
BookController
notBook(controller: 'book', invert: true) {}
- All actions containing 'save' in the action name
saveInActionName(action: '*save*', find: true) {}
- All actions starting with the letter 'b' except for actions beginning with the phrase 'bad*'
actionBeginningWithBButNotBad(action: 'b*', actionExclude: 'bad*', find: true) {}
- Applied to a URI space
someURIs(uri: '/book/**') {}
- Applied to all URIs
allURIs(uri: '/**') {}
filters
code block dictates the order in which they are executed. To control the order of execution between Filters
classes, you can use the dependsOn
property discussed in filter dependencies section.Note: When exclude patterns are used they take precedence over the matching patterns. For example, if action is 'b*' and actionExclude is 'bad*' then actions like 'best' and 'bien' will have that filter applied but actions like 'bad' and 'badlands' will not.