To create a filter create a class that ends with the convention
Filters
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
}
}
Each filter you define within the
filters
block has a name and a scope. The name is the method name and the scope is defined using named arguments. For example if you need to define a filter that applies to all controllers and all actions you can use wildcards:
sampleFilter(controller:'*', action:'*') {
// interceptor definitions
}
The scope of the filter can be one of the following things:
- A controller and/or action name pairing with optional wildcards
- A URI, with Ant path matching syntax
Filter rule attributes:
controller
- controller matching pattern, by default * is replaced with .* and a regex is compiled
action
- action matching pattern, by default * is replaced with .* and a regex is compiled
regex
(true/false) - use regex syntax (don't replace '*' with '.*')
uri
- a uri to match, expressed with as Ant style path (e.g. /book/**)
find
(true/false) - rule matches with partial match (see java.util.regex.Matcher.find())
invert
(true/false) - invert the rule (NOT rule)
Some examples of filters include:
- 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) {}
someURIs(uri:'/book/**') {}
In addition, the order in which you define the filters within the
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.