バーコードを動的に生成すると、最新のコードではなく、常に以前の入力が出力されます。ブラウザの更新ボタンでページを更新すると、正しいバーコードが表示されます。しかし、JSF コマンド ボタンを使用してページを更新しても、以前の結果のままです。どこで間違ったのですか?
JSF 2.1 Primefaces 4.0 Barbecue 1.5 ベータ版 Chrome/Firefox 最新アップデート
<p:graphicImage value="#{barcodeController.createBarcodeByCode(patientController.current.code)}"
style="max-width: 7.5cm; padding: 10px; margin: 10px;" >
</p:graphicImage>
これは、リクエスト スコープを持つ JSF コントローラからのものです。
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;
}
}
参考文献: