我在我的Grails應用程序中使用opencsv將屬性從我的Person域類導出到CSV。但是,我收到以下錯誤:使用HttpServletResponse導出文件進行下載的Grails錯誤
servlet.service()for servlet [default]在上下文中使用path [/ myapp]拋出異常[Request processing failed;嵌套的異常是org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException:處理GroovyPageView時出錯:getOutputStream()已被調用此響應],其根本原因 消息:getOutputStream()已被調用此響應
從在線搜索中,我認爲答案可能是爲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()
}
}
你有沒有在任何地方調用'render'或'redirect'(這是在上面的代碼之外)? –
不,我不... – littleK
嘗試使用充滿數據的CSV和標準模型的gsp模板。 –