我正在尝试使用DynamicImage在同一页面上实现ImageUpload并显示上传的Image。我的问题是,我无法强制刷新p:graphicImage内容并在上传后显示上传的图像。
@ManagedBean(name = "myformbean")
@Controller
@ViewScoped
@Data
public class MyFormBean implements Serializable {
private StreamedContent listImage = null;
public StreamedContent getListImage() {
if (listImage == null) {
try {
listImage = new DefaultStreamedContent(new FileInputStream("E:/t.jpg"), "image/png"); // load a dummy image
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return listImage;
}
public void handleFileUpload(FileUploadEvent event) {
final UploadedFile uploadedFile = event.getFile();
listImage = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFile.getContents()), "image/png");
}
}
在.xhtml文件中:
<p:graphicImage value="#{myformbean.listImage}" />
答案 0 :(得分:10)
如果您的上传工作
您需要做的就是将ID设置为<p:graphicImage
,就像这样
<p:graphicImage id="refreshMe" value="#{myformbean.listImage}" />
并在<p:fileUpload
中设置更新属性以指向图片
<p:fileUpload auto="true" ... update="refreshMe" ....