我在我的 Grails 应用程序中使用opencsv将属性从我的 Person 域类导出到 CSV。但是,我收到以下错误:
Servlet.service() for servlet [default] 在上下文中与路径 [/myapp] 引发异常 [请求处理失败;嵌套异常是 org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause Message: getOutputStream() has already been called for this response
通过在线搜索,我认为答案可能是在某处为 HttpServletResponse 响应实现一些 responseComplete() 方法。但是,我不知道该怎么做。有任何想法吗?这是我的代码:
def export = {
def course = Course.get(params.id)
if(course){
def persons = course ? Person.findAllByCourse(course) : []
response.setHeader("Content-disposition",
"attachment; filename=people.csv")
response.contentType = "text/csv"
def out = response.outputStream
out.withWriter { writer ->
String[] properties = new String[3]
def csvWriter = new CSVWriter(writer)
persons.each { person ->
properties[0] = person.firstName
properties[1] = person.lastName
properties[2] = person.email
properties[3] = person.phone
properties[4] = person.address1
properties[5] = person.address2
properties[6] = person.city
properties[7] = person.state
properties[8] = person.zip5
csvWriter.writeNext(properties)
}
csvWriter.flush()
}
}