Summary
案例 |
参数类型 |
约定的前端变量 |
respond Book.list() |
java.util.List |
bookList |
respond Book.get(1) |
example.Book |
book |
respond( [1,2] ) |
java.util.List |
integerList |
respond( [1,2] as Set ) |
java.util.Set |
integerSet |
respond( [1,2] as Integer[] ) |
Integer[] |
integerArray |
Demo
- 默认的 show 页面,传递一个对象,和一组其他对象。
- 可以选择最合适的类型进行响应
def show(Long id)
{
def layout = Layout.get(id)
def layoutPanel = LayoutPanel.findAllByLayout(layout, [sort: 'displayOrder', order: 'asc'])
respond layout, model: [layoutPanel: layoutPanel] // 默认的 show 页面,传递一个对象,和一组其他对象。
}
// 选择最合适的类型并转换格式进行响应
respond Book.get(1), formats: ['xml', 'json']
Demo
- @Transactional + transactionStatus.setRollbackOnly() + return 保障对象异常的时候不保存数据。
- respond layoutButton.errors, view: 'create', params: params 保障返回错误信息,传回必备的参数。
- flash.message 保障存储成功后页面的提示信息。
@Transactional
def save(LayoutButton layoutButton)
{
if (layoutButton == null)
{
transactionStatus.setRollbackOnly()
notFound()
return
}
if (layoutButton.hasErrors())
{
transactionStatus.setRollbackOnly()
respond layoutButton.errors, view: 'create', params: params
return
}
layoutButton.save(flush: true)
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'layoutButton.label', default: 'LayoutButton'), layoutButton.id])
redirect url: params['targetUri']
}
'*' { respond layoutButton, [status: CREATED] }
}
}
参数
object
必选参数:需要响应的对象,这个是必须有的!
arguments
可选的参数
可选的参数
view
- The view to use in case of HTML rendering(相应的页面)
model
- The model to use in case of HTML rendering(可以相应各种类型的数据)
status
- The response status(相应状态)
formats
- A list of formats to respond with
includes
- Properties to include if rendering with the converters API
excludes
- Properties to exclude if rendering with the converters API