问题描述
以下操作旨在完全绕过Grails视图图层将 bytes
的二进制内容直接写入客户端:
def actionName = {
byte [] bytes = ...
ServletOutputStream out = response.getOutputStream()
out.write(bytes )
out.flush()
out.close()
return false
}
我的印象是 return false
会让Grails完全跳过视图层。然而,看起来并非如此,因为上面的代码仍然使得Grails搜索 /WEB-INF/grails-app/views/controllerName/actionName.jsp
(它失败了因为没有这样的文件存在)。
问题:
- 给定上面的代码,我该如何完全绕过Grails中的视图层? / div>
- 使用
render(contentType: text / html,text:htmlString)
,如。这将绕过视图层。 - 使用
response.contentType =''
>清除内容类型。这也将绕过视图层。但是,请注意,这些内容将传送给最终用户,而Content-Type可能会混淆某些浏览器。
在Grails尝试呈现视图时,如果 response.contentType.startsWith('text / html')
。这似乎是一个已知的错误,请参阅。
以下是两个解决方法:
The following action is meant to write the binary content of bytes
directly to the client completely bypassing the Grails view layer:
def actionName = {
byte[] bytes = ...
ServletOutputStream out = response.getOutputStream()
out.write(bytes)
out.flush()
out.close()
return false
}
I was under the impression that return false
would make Grails completely skip the view layer. However, that appears not to be the case since the above code still makes Grails search for /WEB-INF/grails-app/views/controllerName/actionName.jsp
(which fails with a 404, since no such file exists).
Question:
- Given the code above, how do I completely bypass the view layer in Grails?
It appears as Grails tries to render the view if response.contentType.startsWith('text/html')
. This seems to be a known bug, see GRAILS-1223.
Here are two work arounds:
- Use
render(contentType: "text/html", text: htmlString)
as suggested in GRAILS-1223. This will bypass the view layer. - Clear the content type with
response.contentType = ''
. This will also bypass the view layer. However, note that the content will be served to the end-user without Content-Type which can confuse some browsers.
这篇关于将二进制内容直接写入客户端,绕过Grails视图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!