Grails:我如何在Grails过滤器中设置响应包装? [英] Grails: how do I set up a response wrapper in a Grails filter?

查看:95
本文介绍了Grails:我如何在Grails过滤器中设置响应包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改特定Grails请求的响应内容。如何在请求过滤器中配置ResponseWrapper?



我曾希望它是以下内容,但响应是只读属性:

  class MyFilters {
def filters = {
wrapFoo(controller:'foo',action:'bar'){
before = {
response = new MyResponseWrapper(response)
}
[...]

谢谢!

解决方案

您无法 - Grails过滤器是Spring HandlerInterceptors的包装器,在servlet过滤器之上被进一步调用处理链。如果你想打包响应,你需要使用一个真正的servlet过滤器。



在src / java或src / groovy中创建实现 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆