在我看来,我调用了以下 jQuery 函数:
$.ajax({
url: '${request.contextPath + '/Ticket/passAll'}',
type: 'POST',
data: Data,
success: function(result) {
$('#export_form').submit();
}
});
在控制器中,我正在对传递的数据进行一些过滤和格式化,并在方法结束时将该数据放入会话中:
session["export"] = filteredData
render "success"
如果成功,$.ajax() 提交 export_form 表单,该表单仅调用控制器中的另一个函数,该函数将下载数据设置为附件。
def filter = session["export"].toString()
def tickets = Ticket.findAll("from Ticket as t ${filter}", [])
session["export"] = null
OutputStream outputStream = response.getOutputStream();
outputStream.write(0xEF);
outputStream.write(0xBB);
outputStream.write(0xBF);
response.setHeader("Content-disposition", "attachment; filename=tickets.csv")
response.setContentType("application/vnd.ms-excel:UTF-8")
def outs = response.outputStream
def cols = [:]
outs << "TicketNo;Ticket Details\n"
tickets.each() {
outs << it.ticketNo + ";" + it.ticketDetails
outs << "\n"
}
outs.flush()
outs.close()
这正在开发中,但是当我在 Tomcat 上部署它时,出现以下错误:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response
org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.createGroovyPageException(GroovyPageView.java:205)
org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.handleException(GroovyPageView.java:181)
org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderWithTemplateEngine(GroovyPageView.java:152)
org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderMergedOutputModel(GroovyPageView.java:83)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.codehaus.groovy.grails.web.sitemesh.SpringMVCViewDecorator.render(SpringMVCViewDecorator.java:67)
org.codehaus.groovy.grails.web.sitemesh.SpringMVCViewDecorator.render(SpringMVCViewDecorator.java:52)
org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:160)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
org.codehaus.groovy.grails.plugins.springsecurity.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:40)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.codehaus.groovy.grails.plugins.springsecurity.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:79)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:65)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
gsp_helpdesk_layoutsmain_gsp.run(gsp_helpdesk_layoutsmain_gsp.groovy:47)
编辑:我在页面上使用 jQuery DataTables 和 Twitter Bootstrap 插件。调用导出操作的表格和按钮都在<g:uploadForm>
.
我使用这行代码在 datables 代码中生成按钮:
$("div.additional_filters").html('<g:actionSubmit value="${message(code: "datatables.passAll")}" action="export_test" class="btn btn-primary"/> );
发生了什么以及如何解决这个问题?谢谢你。
我知道这不是正确的方法,但目前必须这样做,所以请多多包涵。