使用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
在他的第一个链接,但如果我点击我的commandButton的文件的内容显示在网页上。可能 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目录中吗?
Do I need to save the file in the tomcat WEB-INF directory first?
不,只要将其直接写入HTTP响应正文,即可获得 ExternalContext#
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.
根据具体示例进行数学运算,得到的结果是getResponseOutputStream()在以下答案中找到:
Do the math based on the concrete examples found in the following answers:
- 如何在JSF备份bean中下载文件?
- a href =http://stackoverflow.com/questions/477886/jsp-generating-excel-spreadsheet-xls-to-download/2154226#2154226> JSP生成Excel电子表格(XLS)以下载
-
- How to stream a file download in 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屋!