I'm starting to program the jsf + primefaces . A long time ago I worked with JSP (java 1.4) and I'm kind of lost.
I want to upload an image and have it displayed right below it.
images.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:p="http://primefaces.org/ui"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{fileUploadView.upload}" mode="advanced"
update="messages Graf" auto="true" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true" />
<p:graphicImage id="Graf" value="#{fileUploadView.imagem}" rendered="#{fileUploadView.imagem != null} "/>
</h:form>
</html>
FileUploadView.java
package br.com.testes;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ejb.Stateful;
import javax.inject.Named;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import lombok.Getter;
import lombok.Setter;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.UploadedFile;
import org.primefaces.model.StreamedContent;
@Named(value = "fileUploadView")
@ManagedBean
@Stateful
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
@Setter
private static StreamedContent imagem;
public StreamedContent getImagem() throws IOException {
return imagem;
}
/**
* Creates a new instance of FileUploadView
*/
public FileUploadView() {
imagem = null;
}
public void upload(FileUploadEvent evt) {
file = evt.getFile();
try {
if (file != null) {
System.out.println(file.getContentType()+"\n\n"+file.getFileName());
InputStream is = file.getInputstream();
imagem = new DefaultStreamedContent(is, file.getContentType());
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
} catch (Exception e) {
FacesMessage message = new FacesMessage(e.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public void upload() {
if (file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
imagem = new DefaultStreamedContent(new ByteArrayInputStream(file.getContents()), file.getContentType());
}
}
public void handleFileUpload(FileUploadEvent event) {
FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
Attempting to upload does not display the image.