h4. Using the render method to output XML
h4. 使用render输出XML
Grails' supports a few different ways to produce XML and JSON responses. The first one covered is via the [render|controllers] method.
Grails提供一些不同的方法来产生XML和JSON响应。第一个是隐式的通过[render|controllers]方法。
The @render@ method can be passed a block of code to do mark-up building in XML:
@render@方法可以传递一个代码块来执行标记生成器产生XML
{code}
def list = {
def results = Book.list()
render(contentType:"text/xml") {
books {
for(b in results) {
book(title:b.title)
}
}
}
}
{code}
The result of this code would be something like:
这段代码的结果将会像这样:
{code:xml}
<books>
<book title="The Stand" />
<book title="The Shining" />
</books>
{code}
Note that you need to be careful to avoid naming conflicts when using mark-up building. For example this code would produce an error:
注意,当你使用标记生成器时,必须小心避免命名冲突。例如,这段代码将产生一个错误:
{code}
def list = {
def books = Book.list() // naming conflict here
render(contentType:"text/xml") {
books {
for(b in results) {
book(title:b.title)
}
}
}
}
{code}
The reason is that there is local variable @books@ which Groovy attempts to invoke as a method.
原因是,这里的一个本地变量@books@企图作为方法被调用。
h4. Using the render method to output JSON
h4. 使用 render 方法 输出 JSON
The @render@ method can also be used to output JSON:
@render@ 方法可以同样被用于输出JSON:
{code}
def list = {
def results = Book.list()
render(contentType:"text/json") {
books {
for(b in results) {
book(title:b.title)
}
}
}
}
{code}
In this case the result would be something along the lines of:
在这种情况下,结果就会是大致相同的:
{code}
[
{title:"The Stand"},
{title:"The Shining"}
]
{code}
Again the same dangers with naming conflicts apply to JSON building.
同样的命名冲突危险适用于JSON生成器。
h4. Automatic XML Marshalling
h4. 自动 XML 列集(Marshalling)(在此附上对于列集(Marshalling)解释:对函数参数进行打包处理得过程,因为指针等数据,必须通过一定得转换,才能被另一组件所理解。可以说列集(Marshalling)是一种数据格式的转换方法。)
Grails also supports automatic marshaling of [domain classes|guide:gorm] to XML via special converters.
Grails同样支持自动列集(Marshalling)[domain classes|guide:gorm]为XML通过特定的转换器。
To start off with import the @grails.converters@ package into your controller:
首先,导入@grails.converters@ 类包到你的控制器(Controllers)中:
{code}
import grails.converters.*
{code}
Now you can use the following highly readable syntax to automatically convert domain classes to XML:
现在,你可以使用下列高度易读的语法来自动转换领域类成XML:
{code}
render Book.list() as XML
{code}
The resulting output would look something like the following::
输出结果看上去会像下列这样:
{code}
<?xml version="1.0" encoding="ISO-8859-1"?>
<list>
<book id="1">
<author>Stephen King</author>
<title>The Stand</title>
</book>
<book id="2">
<author>Stephen King</author>
<title>The Shining</title>
</book>
</list>
{code}
An alternative to using the converters is to use the [codecs|guide:codecs] feature of Grails. The codecs feature provides [encodeAsXML|guide:codecs] and [encodeAsJSON|guide:codecs] methods:
一个使用转换器的替代方法是使用Grails的[codecs|guide:codecs]特性。codecs特性提供了[encodeAsXML|guide:codecs]和[encodeAsJSON|guide:codecs]方法:
{code}
def xml = Book.list().encodeAsXML()
render xml
{code}
For more information on XML marshaling see the section on [REST|guide:REST]
更多的XML 列集(Marshalling)信息见[REST|guide:REST]部分
h4. Automatic JSON Marshalling
h4. 自动JSON列集(Marshalling)
Grails also supports automatic marshaling to JSON via the same mechanism. Simply substitute @XML@ with @JSON@:
Grails同样支持自动列集(Marshalling)为JSON通过同样的机制。简单替代@XML@ 为@JSON@
{code}
render Book.list() as JSON
{code}
The resulting output would look something like the following:
输出结果看上去会像下列这样:
{code}
[
{"id":1,
"class":"Book",
"author":"Stephen King",
"title":"The Stand"},
{"id":2,
"class":"Book",
"author":"Stephen King",
"releaseDate":new Date(1194127343161),
"title":"The Shining"}
]
{code}
Again as an alternative you can use the @encodeAsJSON@ to achieve the same effect.
再次作为一种替代,你可以使用@encodeAsJSON@达到相同的效果
附上满江红的Grails翻译,因为雪灾的缘故,进度可能被我拖后了,也是为了让大家参数进来,如果翻译有什么不正确的地方可以及时提出来,进行更正。
分享到:
相关推荐
群晖 6.1.7 DS3617引导
jetty-6.1.7.jar包下载,为了方面大家
黑群晖 安装的引导版本对比、下载 群晖6.2.2版本: DS 918+ 1.04b 6.2.2 DS 3617xs 1.03b 6.2.2 DS 3615xs 1.03b 6.2.2 群晖6.1.7 DS 916+ 1.02b 6.1.7 DS 3617xs 1.02b 6.1.7 DS 3615xs 1.02b 6.1.7
C#非常棒的反编译工具dnSpy,dnSpy是一款基于.NET的反编译与调试工具,开源免费,能够将.NET开发的Exe和Dll程序集反编译为C#代码,同时支持断点调试和代码二次编辑,如果您只有编译后的程序集,在没有源码的情况下想...
黑群晖6.1.7安装教程,通过视频一对一,一步步教你如何安装
图个轻松6.1.7版本,下载把,绝对无毒无害。
虚拟机安装黑群晖6.1.7synoboot.vmdk引导文件,在新建磁盘选择-使用现有的磁盘后选择压缩包中的synoboot.vmdk文件引导即可。
jun 群辉 6.1.7 引导 1.02b , 用不了6.2 的可以用这个
谷歌定位元素插件
ThinkAdmin后台开发框架 v6.1.7.zip
英文原版hibernate-validator文档。源自官网,由于在线阅读速度过慢,便于学习,遂下载分享
群晖6.1.7 引导文件 容量50M,可用群晖虚拟系统可用群晖虚拟系统ds3617
DS3615xs-6.1.7-1127引导文件。
群晖NAS DSM_DS3615xs6.1.7_23739 引导镜像_DSM【稳定版】
maven-jetty-plugin-6.1.7.jar
群晖6.1.7 引导文件 容量50M,可用群晖虚拟系统可用群晖虚拟系统ds3617
群辉DS3615-V6.1.7 测试虚拟机ovf 格式,导入后即可运行。 本虚拟机仅提供学习群辉操作使用。 若需长时间使用,建议购买正版群辉
群晖6.1.7 ds916引导群晖6.1.7 ds916引导群晖6.1.7 ds916引导
黑群晖6.1.7版本photostation