Grails过滤器语法,或者,如何在Grails之外调用Grails过滤器 [英] Grails filter syntax, or, How to call a Grails Filter outside Grails

查看:146
本文介绍了Grails过滤器语法,或者,如何在Grails之外调用Grails过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails提供在控制器之前运行的过滤器。他们被定义在这样的类中:

  class SecurityFilters {
def filters = {
myFilter(controller:'*',action:'*'){//那些奇怪的冒号是什么?
打印我正在过滤!
//代码是否过滤到这里







这些工作很好,但我想更好地理解语法,因为它看起来不像我以前见过的任何Groovy代码。特别是,以 myFilter 开头的行似乎很奇怪。这是一个称为 myFilter 的方法的定义吗?如果是这样,在每个参数之后,:'*'是什么意思?我认为这可能是一个默认的参数值,但这将是 ='*'。我之前在方法调用中看到了命名参数,但是这不能是方法调用,因为我没有定义 myFilter()任何地方都可以。



如果有人能告诉我如何从一个正常的Groovy类执行过滤代码,我想我会明白好多了。换句话说,如果我有一个包含上面几行的文件MyFilters.groovy,我怎样才能完成这个Groovy代码,以便打印出我正在过滤?

  import MyFilters 
def mf = new MyFilters()
mf.filters.somethingGoesHere // Help!我如何完成这一行,所以它会调用我的过滤代码?


解决方案

以下的Groovy代码会打印 !:

  class SecurityFilters {
def filters = {
myFilter(controller:'*',行动:'*'){//什么是那些奇怪的冒号?
打印我正在过滤!
//代码是否过滤到这里



$ FilterDelegate {
def methodMissing(String methodName,args ){
// methodName == myFilter
// args [0] == [controller:*,action:*]
// args [1] == {printI ()
args [1] .call()
}
}

def sf = new SecurityFilters()
def filtersClosure = sf .filters
filtersClosure.delegate = new FilterDelegate()
filtersClosure.call()

在这个例子中, filters 是一个闭包,它调用一个名为 myFilter 的方法,并传递一个map和一个闭包。你可以把myFilter看作:

$ my $ $ $ $ $ $ $ myFilter([controller:'*',action:'*'],closure)

Map可以包含控制器,动作或uri等键。当Grails尝试确定要调用哪个闭包时,尝试匹配来自HTTP请求的URI时使用通配符(*)。



我对Grails如何处理过滤器是使用委托加载器类。 loader类提供了一个methodMissing方法,并为filter closure内的每个方法调用创建一个FilterConfig。当发出HTTP请求时,Grails将查看所有FilterConfig对象,并尝试查找匹配范围(在映射中查找控制器,操作或uri,并使用正则表达式在通配符上进行匹配)。如果找到匹配项,它将调用传递给Filter类中的方法的闭包。


Grails provides filters that run before your controllers. They're defined in classes that look like this:

class SecurityFilters {
   def filters = {
       myFilter(controller:'*', action:'*') { // What are those weird colons??
           print "I'm filtering!"
           // Code that does the filtering goes here
       }
   }
}

These work great but I would like to understand the syntax better as it doesn't look like any Groovy code I've seen before. In particular, the line above that starts with myFilter seems very odd. Is this a method definition for a method called myFilter? If so, what does :'*' mean after each parameter? I thought it might be a default parameter value but that would be ='*'. I've seen named parameters using colons in method calls before but this couldn't be a method call because I haven't defined myFilter() anywhere else.

I think I'd understand much better if someone could just tell me how to execute the filtering code from a normal Groovy class. In other words, if I have a file MyFilters.groovy that contains the lines above, how could I finish this Groovy code so it prints "I'm filtering"?

import MyFilters
def mf = new MyFilters()
mf.filters.somethingGoesHere // Help! How do I finish this line so it calls my filtering code?

解决方案

The following Groovy code would print "I'm filtering!":

class SecurityFilters {
   def filters = {
       myFilter(controller:'*', action:'*') { // What are those weird colons??
           print "I'm filtering!"
           // Code that does the filtering goes here
       }
   }   
}

class FilterDelegate {
    def methodMissing(String methodName, args) {
        // methodName == myFilter
        // args[0] == [controller:*, action:*]
        // args[1] == {print "I'm filtering!"}
        args[1].call()
    }
}

def sf = new SecurityFilters()
def filtersClosure = sf.filters
filtersClosure.delegate = new FilterDelegate()
filtersClosure.call()

In this example filters is a closure that calls a method named myFilter and passes a map and a closure. You can think of myFilter as:

myFilter([controller:'*', action:'*'], closure)

The Map can contains keys like controller, action or uri. The wildcard (*) is used when Grails attempts to match the URI from the HTTP request when it tries to determine which closure to call.

My understanding of how Grails handles filters is that a delegate loader class is used. The loader class provides a methodMissing method and creates a FilterConfig for each method call inside the filters closure. When a HTTP request is made, Grails looks through all the FilterConfig objects and tries to find a matching scope (looks in the map for a controller, action or uri and uses regexs to match on wildcards). If it finds a match it calls the closure that was passed into the method in the Filter class.

这篇关于Grails过滤器语法,或者,如何在Grails之外调用Grails过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆