在Grails Filter中访问params

时间:2014-11-03 19:02:10

标签: grails filter

当我尝试访问Grail过滤器中的params时,在before {}闭包中调用的实用程序方法中,我得到:

groovy.lang.MissingPropertyException: No such property: params for class: myproject.MyFilters

如何在过滤器中访问等效的params对象?

由于

1 个答案:

答案 0 :(得分:4)

你可以这样做......

// grails-app/conf/paramsinfilter/DemoFilters.groovy
package paramsinfilter

class DemoFilters {

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                // params is available here
                println "Params in before filter: $params"
            }
            after = { Map model ->
                // params is available here
                println "Params in after filter: $params"
            }
            afterView = { Exception e ->
                // params is available here
                println "params in afterView filter: $params"
            }
        }
    }
}

修改

我现在看到问题已被编辑。如果你想在你从过滤器调用的实用程序方法中引用params,你可以根据你真正想做的事情有很多选项,但你很可能想要做的是将params作为参数传递实用方法。

// grails-app/conf/paramsinfilter/DemoFilters.groovy
package paramsinfilter

class DemoFilters {

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                // params is available here
                helper(params)
            }
            after = { Map model ->
                // params is available here
                helper(params)
            }
            afterView = { Exception e ->
                // params is available here
                helper(params)
            }
        }
    }

    private helper(params) {
        println "Params in helper: $params"
    }

}