6.6.1 应用过滤器 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.6.1 应用过滤器
To create a filter create a class that ends with the convention Each filter you define within the The scope of the filter can be one of the following things:
In addition, the order in which you define the filters within the
要创建一个过滤器,只需要在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
}
}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.
grails-app/conf目录下创建一个符合规约以Filters结尾的类即可。在此类中,定义一个名为filters的代码块,用以包含过滤器的定义:class ExampleFilters {
def filters = {
// your filters here
}
}filters代码块内的每一个过滤器有一个名称和作用域。名称就是其方法名,作用域是通过命名参数定义的。比如,要定义一个应用于所有控制器和操作的过滤器,你可以使用通配符:sampleFilter(controller:'*', action:'*') {
// interceptor definitions
}- 一个控制器或者操作名称,支持可选的通配符
- 一个URI,符合Ant路径(path)匹配语法
controller- 控制器匹配模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式controllerExclude- 控制器的排除模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式action- 操作匹配模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式actionExclude- 操作排除模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式regex(true/false) - 使用正则表达式语法(不使用'*'代替'.*')uri- 一个uri匹配,使用Ant风格的路径(path)(比如 /book/**)uriExclude- 一个uri排除匹配,使用Ant风格的路径(path)(比如 /book/**)find(true/false) - 符合部分匹配的规则匹配(更多请参考java.util.regex.Matcher.find())invert(true/false) - 反转规则(不符合此规则的条件)
- 匹配所有的控制器和操作
all(controller: '*', action: '*') {}- 仅仅匹配
BookController
justBook(controller: 'book', action: '*') {}- 匹配所有的控制器,除了
BookController
notBook(controller: 'book', invert: true) {}- 匹配所有操作名包含'save'的操作
saveInActionName(action: '*save*', find: true) {}- 匹配所有操作字母以'b'开头的操作,不过'bad*'除外
actionBeginningWithBButNotBad(action: 'b*', actionExclude: 'bad*', find: true) {}- 应用于一个URI
someURIs(uri: '/book/**') {}- 应用于所有的URIs
allURIs(uri: '/**') {}filters代码块中定义的过滤器顺序就是它们被执行的顺序。要控制Filters类之间的执行顺序,你可以使用dependsOn属性,更多信息将在过滤器的依赖章节讨论。注意:当使用排除模式的时候,其优先级将高于其他的匹配模式。比如一个作用域,其action是'b*'而actionExclude是'bad*',那么操作名称是'best'和'bien'将应用于此过滤器,而操作名是'bad'和'badlands'却没有。

