Unexpected behavior with backing bean method and &

2019-09-18 09:29发布

问题:

(First of all, sorry for my bad english) I'm using JSF 2 + Primefaces 3.5 + Spring 3.2. I have a list of Orders in a p:datatable component.

I want to select one row of the table (works fine, no problem here) and then click a toolbar button to: first, create a PDF file with JasperReports 4.8(no problem here) and then, show a PDF preview in a p:media within a p:dialog component. This is my p:commandButton: (Pay attention to update and oncomplete attributes)

    <p:commandButton id="previewPDF" value="Preview PDF" 
    action="#{ordenBean.printPreview}" icon="ui-icon-print" 
    update="pdfDialog" oncomplete="PF('dlg_pdf').show();" />

And this is the simple dialog:

<p:dialog id="pdfDialog" widgetVar="dlg_pdf" modal="true" draggable="false" header="PDF Preview" width="75%" position="center" height="600px">
    <p:media player="pdf" value="/pdf/preview.pdf" width="100%" height="100%" />
</p:dialog>

A p:ajaxstatus component to show a loading dialog. Works fine.

<p:ajaxStatus onstart="PF('statusDialog').show();" onsuccess="PF('statusDialog').hide();"/>

<p:dialog modal="true" widgetVar="statusDialog" header="Processing..." draggable="false" closable="false">  
    <p:graphicImage value="/images/loading.gif" />  
</p:dialog>  

All works fine, the PDF file is created, the dialog shows up, but sometimes I select a row, press the button and the dialog shows the previously created PDF (of the previously selected row), that is to say the dialog loads the PDF file before the new one has been created (i.e. before the printPreview() process has been complete). This is the bean method printPreview() that create the PDF file: (Works fine too)

public void printPreview() {
    try {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ID", orden.getId());
        map.put("LOCALE", java.util.Locale.US);
        DataSource ds = (DataSource) ApplicationContextProvider.getApplicationContext().getBean("dataSource", DataSource.class);
        Connection cn = ds.getConnection();
        Resource rsc = ApplicationContextProvider.getApplicationContext().getResource("classpath:reports/preview.jasper");
        String outFileName = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + "pdf\\preview.pdf";
        JasperPrint jasperPrint= JasperFillManager.fillReport((JasperReport) JRLoader.loadObject(rsc.getInputStream()), map, cn);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName);
    } catch (Exception ex) {
        // handling exceptions
    }
}

What's wrong? After the dialog show up and loads the previous PDF, I navigate to the path where the PDF file was created and the file is ok (the Order that I actually selected).