早期用gils冲洗gsp

时间:2013-11-24 14:50:20

标签: grails gsp flush

我正在尝试使用gsp Grails 2.2.4进行早期冲洗但不起作用。

<%
response.flushBuffer();
%>

<%

org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest webRequest =  (org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest) org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes()

webRequest.getOut().flush();

%>

2 个答案:

答案 0 :(得分:0)

Sitemesh使用text / html内容类型缓冲响应响应。您可能必须从sitemesh中排除uri。有关详细信息,请参阅http://jira.grails.org/browse/GRAILS-5773

答案 1 :(得分:0)

早期冲洗

负责无法手动刷新响应的是GrailsPageFilter过滤器,它包装响应对象,以便他可以管理响应缓冲区。

您可以通过在src / java中创建新过滤器来跳过此过滤器:

package com.test;

import java.io.IOException;
import javax.servlet.ServletException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BypassGrailsPageFilter implements Filter {

    private static final String ALREADY_APPLIED_KEY = "com.opensymphony.sitemesh.APPLIED_ONCE";

    public void init(FilterConfig config) throws ServletException {}

    public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) rq;

        req.setAttribute(ALREADY_APPLIED_KEY, Boolean.TRUE);

        chain.doFilter(req, rs);
    }

    public void destroy() {
    }
}

在sitemesh过滤之前将其添加到您的web.xml(位于src / templates / war中)。

<filter>
    <filter-name>nositemesh</filter-name>
    <filter-class>com.test.BypassGrailsPageFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>nositemesh</filter-name>
    <url-pattern>/path/to/controller/*</url-pattern>
</filter-mapping>

控制器示例:

class EarlyFlushController {

    def test() {
        def out = response.outputStream

        out << '<html>'
        out << '<body>'

        out.flush()

        (1..10).each { i ->
            out << i
            out.flush()
            Thread.sleep(1000)
        }

        out << '</body>'
        out << '</html>'
        out.flush()

        out.close()

        response.status = 200
    }
}

注意:如果您无法找到您的web.xml,请尝试在您的应用目录上运行grails install-template


SiteMesh的

您仍然可以使用g.render()使用grails模板系统并将其输出发送到响应输出。