// 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
def theShining = new Book(title: 'The Shining', author: 'Stephen King')
render(template: "book", model: [book: theShining])
// 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
def theShining = new Book(title: 'The Shining', author: 'Stephen King')
render(template: "book", bean: theShining)
// render the view with the specified model
def theShining = new Book(title: 'The Shining', author: 'Stephen King')
render(view: "viewName", model: [book: theShining])
// 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: "application/json") {
book(title: b.title, author: b.author)
}
// render with status code
render(status: 503, text: "Failed to update book ${b.id}")
// render a file
render(file: new File(absolutePath), fileName: "book.pdf")
render
用途
從簡單文字回應、檢視和範本呈現不同形式的回應。
範例
說明
一個多用途的方法,用於呈現回應給客戶端,最適合用幾個範例來說明!警告 - 此方法並不總是支援多個參數。例如,如果您同時指定集合和模型,則會忽略模型參數。參數
參數
-
text
(選用) - 要呈現的文字 -
builder
(選用) - 呈現標記時要使用的建構器 -
view
(選用) - 要委派呈現的檢視 -
template
(選用) - 要呈現的範本。範本通常是 HTML 片段,且檔案以底線開頭(「_」)。這主要用於 AJAX 要求。 -
layout
(選用) - 要用於回應的版面配置 -
var
(選用) - 要傳遞到範本的變數名稱,如果未指定,則預設為 Groovy 預設引數「it」 -
bean
(選用) - 要用於呈現的 bean -
model
(可選)- 渲染中使用的模型 -
collection
(可選)- 針對集合中的每個項目渲染範本 -
contentType
(可選)- 回應的 contentType -
encoding
(可選)- 回應的編碼 -
plugin
(可選)- 尋找範本的插件 -
status
(可選)- 要使用的 HTTP 狀態碼 -
file
(可選)- 要隨回應發送的 byte[]、java.io.File 或 inputStream -
fileName
(可選)- 渲染檔案時指定附件檔案名稱。