(Quick Reference)

6.3.5 标签命名空间 - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.3.5 标签命名空间

By default, tags are added to the default Grails namespace and are used with the g: prefix in GSP pages. However, you can specify a different namespace by adding a static property to your TagLib class:

class SimpleTagLib {
    static namespace = "my"

def example = { attrs -> … } }

Here we have specified a namespace of my and hence the tags in this tag lib must then be referenced from GSP pages like this:

<my:example name="..." />

where the prefix is the same as the value of the static namespace property. Namespaces are particularly useful for plugins.

Tags within namespaces can be invoked as methods using the namespace as a prefix to the method call:

out << my.example(name:"foo")

This works from GSP, controllers or tag libraries

一般情况下,标签使用Grails的缺省命名空间,并且在GSP页面中使用g:前缀。但是你也可以通过在TagLib类中增加一个静态属性来指定另外一个命名空间:

class SimpleTagLib {
    static namespace = "my"

def example = { attrs -> … } }

此处我们将namespace指定为my,因此此标签库的标签在GSP页面中必须像如下所示那样引用:

<my:example name="..." />

前缀部分跟静态属性namespace的值是一样的。命名空间对插件来说特别有用。

带命名空间的标签也可以以方法的方式调用,需要将其命名空间作为前缀赋给方法调用:

out << my.example(name:"foo")

这将在GSP、控制器或者标签库中有效