在 grails 2.xxx 中使用 respond() 而不是 render() 有什么优势?
当我尝试生成域的 Controller 和 View 时,我发现了 respond()。
就像在 create()
它的代码如下:
respond new DomainObject(params);
我还发现了 request.withFormat(),对我来说有点奇怪
droppingInstance.save flush:true;
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'dropping.label', default: 'Dropping'), droppingInstance.id])
redirect droppingInstance
}
'*' { respond droppingInstance, [status: CREATED] }
}
我已经阅读了文档,但没有发现有关多平台等的任何线索...
http://grails.org/doc/latest/ref/Controllers/request.html
http://grails.org/doc/latest/ref/Controllers/respond.html
还有为什么不使用重定向来显示数据?
有人可以解释这两种方法吗? :)
最佳答案
Whats is the advantage of using respond() over render() in grails 2.xxx ?
respond
也不是这样。或 render
比其他更好。它们是对不同事物有用的两种不同方法。最大的区别是 respond
在构建 REST 接口(interface)并且您希望使用请求 header 中基于不同内容的信息进行响应时非常有用(例如)。例如,如果您的 Controller 操作执行以下操作:
respond new DomainObject(params)
您还没有在那里表达响应应该是什么格式。这可能会导致 GSP 被渲染,它可能会导致域实例被序列化为 JSON、XML 或其他格式。在构建需要支持多种内容类型的 REST 接口(interface)并且您不想用一堆命令式逻辑来处理这些内容类型时,这非常灵活和有用。详情请访问 http://grails.org/doc/latest/guide/single.html#REST和 http://grails.org/doc/latest/ref/Controllers/respond.html .
also I found request(), a little bit strage to me
您在什么情况下看到了对
request()
的调用? ?also why not using redirect to display the data ?
因为
redirect
不显示数据。重定向导致浏览器重定向到其他地方。其他地方可能会显示 render
的数据, respond
或者做它需要做的任何事情,但所有 redirect
确实是说“去别的地方”。
关于grails - 在 Grails 2.4.x 中使用 Respond Over Render 有什么优势(以及 Request() 是什么)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220111/