render
用途
把简单的文本响应渲染到特定的视图或模板。
举例
// renders text to response
render "some text"// renders text for a specified content-type/encoding
render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8")// render a template to the response for the specified model
render(template:"book",model:[book:new Book(title:'The Shining',author:'Stephen King')])// render each item in the collection using the specified template
render(template:"book",collection:[b1, b2, b3])// render a template to the response for the specified bean
render(template:"book",bean:new Book(title:'The Shining',author:'Stephen King'))// render the view with the specified model
render(view:"viewName",model:[book:new Book(author:'Stephen King')])// render the view with the controller as the model
render(view:"viewName")// render some markup to the response
render {
div(id:"myDiv", "some text inside the div")
}// render some XML markup to the response
render(contentType:"text/xml") {
books {
for(b in books) {
book(title:b.title,author:b.author)
}
}
}// render a JSON ( http://www.json.org ) response with the builder attribute:
render(contentType:"text/json") {
book(title:b.title,author:b.author)}// Automatic marshalling of XML and JSON
import grails.converters.*
…
render Book.list(params) as JSON
render Book.get(params.id) as XML
描述
上面列举了多种把相应渲染给客户端的方法。警告:Grails 0.5中,这个方法不支持多个参数!假如你指定了2个参数:collection和model,后面的model参数会被忽略。
参数:
text
(可选) - 要渲染的文本
builder
(可选) - 渲染时要用到的builder(builder to use when rendering markup)
view
(可选) - 渲染用的目标视图
template
(可选) - 渲染用的目标模板
var
(可选) - 传给模板的变量名,如果没有指定用Groovy默认选项
bean
(可选) - 需要渲染的bean
model
(可选) - 渲染要用的model
collection
(可选) - 存放渲染模板的数据的集合
contentType
(可选) - 响应的contentType
encoding
(可选) - 响应的编码
converter
(不需要命名的第一个参数) - 渲染需要用到的Converter
plugin
(可选) - 包涵渲染用到的模板的插件