1

When I generate barcode dynamically, it always prints the previous input, not the latest code. When I refresh the page with refresh button on the browser, the correct barcode is displayed. But If I refresh the page with a JSF commandbutton, still the previous result. Where have I gone wrong ?

JSF 2.1 Primefaces 4.0 Barbecue 1.5 beta Chrome/Firefox Latest Updates

<p:graphicImage value="#{barcodeController.createBarcodeByCode(patientController.current.code)}" 
                style="max-width: 7.5cm; padding: 10px; margin: 10px;" >
</p:graphicImage>

This is from the JSF controller with request scope.

public StreamedContent createBarcodeByCode(String code) {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
        return new DefaultStreamedContent();
    } else {
        barcode = null;
        System.out.println("code = " + code);
        if (code == null || code.trim().equals("")) {
            return null;
        }
        File barcodeFile = new File(code);
        try {
            BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128C(code), barcodeFile);
            barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
        } catch (Exception ex) {
            System.out.println("ex = " + ex.getMessage());
        }

        return barcode;
    }
}

References:

1. Dynamic StreamContent Answer by BalusC

2. Dynamic Barcode with Primefaces

4

1 回答 1

3

设置cache=false在图形图像上。

于 2014-08-11T09:52:57.253 回答