我不知道如何下载CSV文件.CSV将在运行时生成.我是否需要先将文件保存在tomcat WEB-INF目录中?我正在使用JSF 1.2.
顺便说一下,这种任务最受欢迎的JSF组件是什么?
编辑(05.05.2012 - 15:53)
我尝试了BalusC他的第一个链接中说明的解决方案,但如果我点击我的commandButton,该文件的内容将显示在网页上.也许这有问题mimetype?
XHTML文件:
<a4j:form>
<a4j:commandButton action="#{surveyEvaluationBean.doDataExport}" value="#{msg.srvExportButton}" />
</a4j:form>
Run Code Online (Sandbox Code Playgroud)
主豆:
public String doDataExport() {
try {
export.downloadFile();
} catch (SurveyException e) {
hasErrors = true;
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
出口豆类:
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 …Run Code Online (Sandbox Code Playgroud) 我创建了一个,SessionListener但是它不起作用,因为我不知道如何在中注册它faces-config.xml。我尝试了以下方法:
会话监听器
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent arg0) {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String param = request.getParameter("language");
IDAdminLanguage idl = (IDAdminLanguage) JSFUtils.resolve("#{languageBean}");
if (param != null && param.length() > 0) {
idl.setLanguage(param);
} else {
idl.setLanguage("de");
}
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) { }
Run Code Online (Sandbox Code Playgroud)
}
faces-config
<listener>
<listener-class>ch.idadmin.util.SessionListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)