スタック: JasperReportsを使用したJBoss AS上のJSF + PrimeFaces
JasperReports を使用して、次の 3 つの手順で PDF 形式にエクスポートするパターンを使用しています。
[1]戦争中の道から編集されたジャスパーレポートを入手する
[2]セッションにJasperPrintオブジェクトを配置する
[3] PdfServletのURLにリダイレクト
したがって、ユーザーが GUI からp:commandButtonをクリックすると、次のコード例のように [1]、[2]、および [3] を通過するバッキング Bean のメソッドが呼び出されます。
xhtml ファイル:
<p:commandButton ajax="false" action="#{indexController.exportPDF}" value="Export PDF"/>
バッキング Bean コード:
private void putPrintObjectInSession() throws JRException {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
ServletContext context = (ServletContext) externalContext.getContext();
String reportFileName = context.getRealPath("/reports/PrimeNumbersReport.jasper");
File reportFile = new File(reportFileName);
if (!reportFile.exists())
throw new JRRuntimeException(".jasper file not found in the war.");
Map parameters = new HashMap();
parameters.put("ReportTitle", "2nd Prime Numbers Report");
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint =
JasperFillManager.fillReport(
reportFileName,
parameters,
getSQLConnection()
);
((HttpSession) externalContext.getSession(false)).setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
}
public String exportPDF() throws IOException, JRException {
putPrintObjectInSession();
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.redirect("servlets/pdf");
return null;
}
2 つの質問があります。
[i] このアプローチで明らかなコードの臭いや制限はありますか?
[ii] 上記のコード例では、Chrome と Conkeror の両方がレポートを保存できますが、ファイルを保存するためにユーザーに提示するデフォルトのファイル名は単に「pdf」です。それを意味のある名前 (例: "report-2012-08-23c.pdf") に設定するにはどうすればよいですか?