(Quick Reference)

6.2.2.6 标签的方法调用 - Reference Documentation

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

Version: null

6.2.2.6 标签的方法调用

One major different between GSP tags and other tagging technologies is that GSP tags can be called as either regular tags or as method calls from controllers, tag libraries or GSP views.

Tags as method calls from GSPs

Tags return their results as a String-like object (a StreamCharBuffer which has all of the same methods as String) instead of writing directly to the response when called as methods. For example:

Static Resource: ${createLinkTo(dir: "images", file: "logo.jpg")}

This is particularly useful for using a tag within an attribute:

<img src="${createLinkTo(dir: 'images', file: 'logo.jpg')}" />

In view technologies that don't support this feature you have to nest tags within tags, which becomes messy quickly and often has an adverse effect of WYSWIG tools such as Dreamweaver that attempt to render the mark-up as it is not well-formed:

<img src="<g:createLinkTo dir="images" file="logo.jpg" />" />

One major different between GSP tags and other tagging technologies is that GSP tags can be called as either regular tags or as method calls from controllers, tag libraries or GSP views.

在GSP中以方法调用标签

当标签以方法的方式调用时,其返回一个类似String(一个StreamCharBuffer,有着跟String完全相同的方法)的对象,而不是直接写回到响应器。比如:

Static Resource: ${createLinkTo(dir: "images", file: "logo.jpg")}

这在一个属性内使用标签的时候特别有用:

<img src="${createLinkTo(dir: 'images', file: 'logo.jpg')}" />

在视图技术中,标签内嵌套标签是不被支持的,因为那将会很快导致混乱,而且像Dreamweaver这样所见即所得(WYSWIG)的工具产生不利的效果,因为那会破坏标签的结构良好性:

<img src="<g:createLinkTo dir="images" file="logo.jpg" />" />

Tags as method calls from Controllers and Tag Libraries

You can also invoke tags from controllers and tag libraries. Tags within the default g: namespace can be invoked without the prefix and a StreamCharBuffer result is returned:

def imageLocation = createLinkTo(dir:"images", file:"logo.jpg").toString()

Prefix the namespace to avoid naming conflicts:

def imageLocation = g.createLinkTo(dir:"images", file:"logo.jpg").toString()

For tags that use a custom namespace, use that prefix for the method call. For example (from the FCK Editor plugin):

def editor = fckeditor.editor(name: "text", width: "100%", height: "400")

在控制器和标签库中的以方法调用标签

你可以可以在控制器和标签库中调用标签。命名空间g:的标签调用可以忽略其前缀,并且一个StreamCharBuffer类型的结果被返回:

def imageLocation = createLinkTo(dir:"images", file:"logo.jpg").toString()

命名空间前缀是用以避免名称冲突的:

def imageLocation = g.createLinkTo(dir:"images", file:"logo.jpg").toString()

对于那些使用自定义命名空间的标签,在以方法调用时要使用其前缀。比如(来自FCK 编辑器插件):

def editor = fckeditor.editor(name: "text", width: "100%", height: "400")