Grails 1.1 GSP 映射通配符

Grails的URL映射机制同样支持通配符映射。

例如,考虑下面的映射:static mappings = { "/images/*.jpg"(controller:"image") }这个映射将匹配所有images路径下像/image/logo.jpg这样的jpg。

当然你可以通过一个变量来达到同样的效果:static mappings = { "/images/$name.jpg"(controller:"image") }然而,你可以使用双通配符来匹配多于一个层次之外的:static mappings = { "/images/**.jpg"(controller:"image") }这样的话,这个映射将不但匹配/image/logo.jpg而且匹配/image/other/logo.jpg。

更好的是你可以使用一个双通配符变量:static mappings = { // will match /image/logo.jpg and /image/other/logo.jpg "/images/$name**.jpg"(controller:"image") }这样的话,它将储存路径,从params 对象获得命名参数里的name通配符 :def name = params.name println name // prints "logo" or "other/logo"如果你使用通配符URL mappings,那么你可以排除某些来自Grails的URL mapping进程URIs. 实现这个你可以在UrlMappings.groovy类中设置excludes :class UrlMappings = { static excludes = ["/images/**", "/css/**"] static mappings = { … } }这样,Grails不为匹配任何以 /images 或 /css开头的URLs.