Grails:为 GSP 生成文本文件

Grails : Generate text file for GSP

我正在使用 Grails 2.3.6 应用程序。

我尝试了很多不同的方法来生成 PDF,但大多数都失败了。

是否可以生成GSP 文件内容的文本文件?然后有一个名为 EXPORT 的按钮,当用户单击该按钮时,文本文件将下载到那里的系统中。

是否可以通过传递 GSP 文件的 URL 来实现?

这很简单。您只需在控制器的渲染方法中指定响应类型 (text/plain)。您可以在 gsp 文件中使用纯文本,并在需要的地方使用标签。

def textFile = {
    response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')

    render view: 'textFile', contentType: 'text/plain'
}

textFile.gsp:

Dear ${name}, This is a text file.

至于pdf,我推荐非常好的grails rendering plugin