Spring MVC:如何修改从 spring 控制器发送的响应? [英] Spring MVC: How to modify response sent from spring controller?

查看:62
本文介绍了Spring MVC:如何修改从 spring 控制器发送的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Spring MVC 3.1.3 和使用 Dojo 1.4 开发的 UI 的应用程序.该应用程序很少有控制器来处理通过 dojo.io.iframe.send 上传的二进制文件.控制器发送一个必须用

I have an app with Spring MVC 3.1.3 and the UI developed with Dojo 1.4. The application has few controllers which handle binary files uploaded through dojo.io.iframe.send. The controller sends a json response which has to be surrounded with

<html><body><textarea>{my json response}</textarea></body></html>.

我已经在 web.xml 中定义了一个自定义过滤器:

I've implemented a custom filter defined in web.xml:

<filter>
    <filter-name>dojoIframeFilter</filter-name>
    <filter-class>com.app.web.MultipartAjaxFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>dojoIframeFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

过滤器的 doFilter 有这种行为,取自 http://www.oracle.com/technetwork/java/filters-137243.html

The filter's doFilter has this behaviour, taken from http://www.oracle.com/technetwork/java/filters-137243.html

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {

    if (request.getContentType() != null
            && request.getContentType().contains("multipart/form-data")) {
        CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);

        chain.doFilter(request, wrapper);
        log.info(wrapper.toString());
        //Modify response here
    } 
    else {
        chain.doFilter(request, response);
    }
}

包装器的输出为空.我还尝试了许多其他组合,例如为 spring 的调度程序 servlet 放置自定义过滤器,取消 doFilter 中的 if 块,但这些都不起作用.我还尝试编写一个 Spring 拦截器,但也失败了.有人可以提出任何其他想法吗?

The wrapper's output is empty. I've also tried many other combinations such as placing the custom filter for spring's dispatcher servlet, doing away with the if block inside doFilter none of which works. I also tried writing a Spring interceptor which also failed. Could anybody please suggest any other idea ?

提前致谢.

更新:我禁用了 spring 安全性并使用普通 spring mvc 进行了测试,但问题仍然存在.我修改了标题和问题描述.

UPDATE: I disabled spring security and tested with plain spring mvc but the problem remains. I have modified the title and the question description.

推荐答案

朋友们,为了成功修改 ServletResponse,我们还需要对 ServletOutputStream 进行子类化.请参阅 http://docstore.mik.ua/orelly/xml/jxslt/ch08_04.htm 获取详细解决方案.

Friends, we need to subclass ServletOutputStream also in addition to in order to successfully modify the ServletResponse. Please see http://docstore.mik.ua/orelly/xml/jxslt/ch08_04.htm for a detailed solution.

这篇关于Spring MVC:如何修改从 spring 控制器发送的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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