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: