Grails:我如何在Grails过滤器中设置响应包装? [英] Grails: how do I set up a response wrapper in a Grails filter?
问题描述
我想修改特定Grails请求的响应内容。如何在请求过滤器中配置ResponseWrapper?
我曾希望它是以下内容,但响应是只读属性:
class MyFilters {
def filters = {
wrapFoo(controller:'foo',action:'bar'){
before = {
response = new MyResponseWrapper(response)
}
[...]
谢谢!
您无法 - Grails过滤器是Spring HandlerInterceptors的包装器,在servlet过滤器之上被进一步调用处理链。如果你想打包响应,你需要使用一个真正的servlet过滤器。
javax.servlet.Filter
并将其注册到web.xml中,就像在非Grails应用程序中一样。要访问web.xml,运行 grails install-templates
,然后编辑src / templates / war中的文件I want to modify the response content of specific Grails requests. How do I configure a ResponseWrapper in a request filter?
I had hoped it would be the following, but the response is a read-only property:
class MyFilters {
def filters = {
wrapFoo(controller:'foo', action:'bar') {
before = {
response = new MyResponseWrapper(response)
}
[...]
Thanks!
You can't - Grails filters are wrappers for Spring HandlerInterceptors and are invoked further up the processing chain than servlet filters. If you want to wrap the response you need to use a real servlet filter.
Create the class in src/java or src/groovy that implements javax.servlet.Filter
and register it in web.xml like you would in a non-Grails application. To get access to web.xml run grails install-templates
and edit the file in src/templates/war
这篇关于Grails:我如何在Grails过滤器中设置响应包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!