答案 0 :(得分:1)
可以这样做。在指向bean的操作方法的页面上添加h:commandLink
:
<h:commandLink value="download report" action="#{myBean.downloadReport}" />
在您的bean中,您应该生成报告并将其写入响应的OutputStream
:
public class MyBean {
public String downloadReport() {
// get HttpServletResponse
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) context.getExternalContext().getResponse();
// set correct content type
response.setContentType("application/pdf");
// get OutputStream
OutputStream stream = response.getOutputStream();
// TODO: generate PDF and write the report to this output stream
// mark response as completed
context.responseComplete();
return null;
}
}