在grails 2.xxx中使用response()优于render()是什么?
当我尝试生成控制器和域的视图时,我发现了response()。
喜欢在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
为什么不使用重定向来显示数据呢?
有人可以解释这两种方法吗? :)
答案 0 :(得分:1)
在grails中使用response()优于render()是什么好处 2.xxx?
情况并非respond
或render
优于另一种情况。它们是两种不同的方法,可用于不同的事物。最大的区别在于respond
在构建REST接口时非常有用,并且您希望使用请求标头中的不同基于内容的信息进行响应(例如)。
例如,如果您的控制器操作执行如下操作:
respond new DomainObject(params)
您尚未表示响应应采用何种格式。这可能导致呈现GSP,这可能导致域实例被序列化为JSON,XML或其他格式。在构建需要支持多种内容类型的REST接口时,这非常灵活且有用,并且您不希望使用一堆命令性逻辑来处理这些内容类型。有关详细信息,请访问http://grails.org/doc/latest/guide/single.html#REST和http://grails.org/doc/latest/ref/Controllers/respond.html。
我也找到了request(),对我来说有点噱头
您在什么情况下看到了request()
的电话?
为什么不使用重定向来显示数据?
因为redirect
不显示数据。重定向导致浏览器重定向到其他位置。其他地方可能会显示render
,respond
的数据或做任何需要做的事情,但redirect
所做的只是“去其他地方”。