My conditions: Java 7/JSF 2.2/PrimeFaces 6.1
I have an edit page that should allow loading an image and save it. To make things look pretty, the image just loaded should be presented before the user clicks save.
A fragment of my page is:
<h:form enctype="multipart/form-data" id="form">
<h2 class="page-header">Image load</h2>
<p:graphicImage id="image" required="true" value="#{bannerEditMB.image}" width="240" height="160"/>
<p:fileUpload id="imageLoad" fileUploadListener="#{bannerEditMB.imageLoad}"
auto="true" mode="advanced" update="image"
label="#{messages['banner.image.label']}"
allowTypes="/(\.|\/)(jpe?g)$/"
invalidFileMessage="Only .jpeg e .jpg" icon="fa fa-file-image-o"/>
<p:commandButton action="#{bannerEditMB.insert}" ajax="false" icon="fa fa-check" id="buttonAdd" rendered="#{not bannerEditMB.updateMode}"/>
<p:commandButton action="#{bannerEditMB.update}" ajax="false" icon="fa fa-check" id="buttonUpdate" rendered="#{bannerEditMB.updateMode}"/>
<p:commandButton action="#{bannerEditMB.cancel}" ajax="false" icon="fa fa-close" id="buttonCancel" immediate="true"/>
</h:form>
The (view scoped) backing bean is:
@ViewController
public class BannerEditMB extends AbstractEditPageBean<Banner, Long> {
private static final long serialVersionUID = 1L;
@Inject
private BannerBC bc;
private StreamedContent image = new DefaultStreamedContent();
@Override
public Banner handleLoad(Long id) {
Banner bean = bc.load(id);
imagem = new ByteArrayContent(bean.getImagem());
return bean;
}
public void imageLoad(FileUploadEvent event)
throws IOException {
byte[] content = event.getFile().getContents();
getBean().setImage(content);
image = new ByteArrayContent(content);
}
public StreamedContent getImage() {
return image;
}
}
The problem is that when I click the load button, everything works, including the imageLoad()
method, that correctly sets the content to the object being edited. In fact, if I click a save button, the corresponding database row is updated as expected and the just loaded image is saved in DB.
Debugging shows that the getImage()
method is called twice after the image is loaded, but nothing changes on screen and the user can´t see the just loaded image.
My code is inspired in PrimeFaces 6.1 User Guide that presents a similar code.