我有一个图表(饼图),用于可视化一些统计信息。 我想在此组件上添加ajax下载操作,以下载从后备bean构建的excel文件。触发事件“ itemSelect”时必须执行此操作。
xhtml:
<h:form id="formRelanceDbePieChartId">
<p:panel styleClass="lvl-2" id="widgets-statistique-dbe" toggleable="true">
<f:facet name="header">Statistiques DBE</f:facet>
<p:chart type="pie" model="#{widgetsBacking.pieModelRelanceDbe}">
<p:ajax event="itemSelect" listener="#{relanceDbeControleur.test()}" />
</p:chart>
</p:panel>
</h:form>
Bean:
public void test() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
BufferedInputStream input = null;
BufferedOutputStream output = null;
List<RegistreRelanceDbeDto> listRelanceDbeDto = relanceDbeFacade.recupererListStockDbeCollecter();
if (CollectionUtils.isEmpty(listRelanceDbeDto)) {
logger.fine(() -> "TDB exporterStockDbeAcollecter : La liste listRelanceDbeDto est vide");
}
String nomFeuille = "stockDbeACollecter";
try {
byte[] bytes = exportExcelService.genererExcel(listRelanceDbeDto, nomFeuille, null);
input = new BufferedInputStream(new SharedByteArrayInputStream(bytes), 10240);
response.reset();
response.setHeader("Content-Type", "charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + "liste_stock_dbe_collecter.xlsx" + "\"");
output = new BufferedOutputStream(response.getOutputStream(), 10240);
byte[] buffer = new byte[10240];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(output);
close(input);
}
FacesContext.getCurrentInstance().responseComplete();
FacesContext.getCurrentInstance().renderResponse();
}
当我单击饼图时,会触发test()方法,但不会显示保存对话框。