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参数会被忽略。

参数: