使用JSF下载CSV文件 [英] Downloading a CSV file using JSF
问题描述
我不知道如何下载CSV文件。 CSV将在运行时生成。首先需要将文件保存在tomcat WEB-INF目录中吗?我正在使用JSF 1.2。
I don't know how to download a CSV file. The CSV will be generated at runtime. Do I need to save the file in the tomcat WEB-INF directory first? I'm using JSF 1.2.
顺便说一下,这种任务的最受欢迎的JSF组件是什么?
By the way, what's the favored JSF component for this kind of task?
编辑(05.05.2012 - 15:53)
Edit (05.05.2012 - 15:53)
我尝试了解决方案 BalusC
在他的第一个链接,但如果我点击我的命令按钮,该文件的内容将显示在网页上。可能是 mimetype
有问题?
I tried the solution BalusC
stated in his first link, but if I click on my commandButton the content of the file is displayed on the webpage. Maybe there's a problem with the mimetype
?
xhtml文件:
<a4j:form>
<a4j:commandButton action="#{surveyEvaluationBean.doDataExport}" value="#{msg.srvExportButton}" />
</a4j:form>
main bean:
main bean:
public String doDataExport() {
try {
export.downloadFile();
} catch (SurveyException e) {
hasErrors = true;
}
return "";
}
export-bean:
export-bean:
public void downloadFile() throws SurveyException {
try {
String filename = "analysis.csv";
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
response.reset();
response.setContentType("text/comma-separated-values");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
OutputStream output = response.getOutputStream();
// writing just sample data
List<String> strings = new ArrayList<String>();
strings.add("filename" + ";" + "description" + "\n");
strings.add(filename + ";" + "this is just a test" + "\n");
for (String s : strings) {
output.write(s.getBytes());
}
output.flush();
output.close();
fc.responseComplete();
} catch (IOException e) {
throw new SurveyException("an error occurred");
}
}
编辑(05.05.2012 - 16:27)
Edit (05.05.2012 - 16:27)
我解决了我的问题。我必须使用< h:commandButton>
而不是< a4j:commandButton>
,现在它可以工作! / p>
I solved my problem. I have to use <h:commandButton>
instead of <a4j:commandButton>
and now it works!
推荐答案
我需要先将文件保存在tomcat WEB-INF目录中吗?
不,只要直接写入HTTP响应正文,就可以通过 ExternalContext# getResponseOutputStream()
在设置了正确的响应头后,告知浏览器将要检索的内容。
No, just write it straight to the HTTP response body as you obtain by ExternalContext#getResponseOutputStream()
after having set the proper response headers which tells the browser what it's going to retrieve.
根据具体示例进行数学在以下答案中找到:
Do the math based on the concrete examples found in the following answers:
- 如何从JSF备份bean提供文件下载
- < a href =https://stackoverflow.com/questions/477886/jsp-generating-excel-spreadsheet-xls-to-download/2154226#2154226> JSP生成Excel电子表格(XL S)下载
- How to provide a file download from a JSF backing bean?
- JSP generating Excel spreadsheet (XLS) to download
基本上:
List<List<Object>> csv = createItSomehow();
writeCsv(csv, ';', ec.getResponseOutputStream());
顺便说一下,这种任务最喜欢的jsf组件是什么?
这是主观的。但无论如何,我们正在使用 < p:dataExporter>
完全满意。
This is subjective. But anyway, we're using <p:dataExporter>
to full satisfaction.
这篇关于使用JSF下载CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!