I'm trying to get a really simple example I found online to work, I've tried searching everywhere but can't seem to find a solution.
This is the bean that I'm using:
@Named("uploadBean")
@RequestScoped
public class UploadBean {
private Part file;
private String fileContent;
public void upload() {
try {
fileContent = new Scanner(file.getInputStream())
.useDelimiter("\\A").next();
} catch (IOException e) {
// Error handling
}
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Uploaded!"));
}
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
public String getFileContent() {
return fileContent;
}
public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
}
This is the form that uses it:
<h:form id="file-upload-test" enctype="multipart/form-data">
<h:inputFile id="in-file" value="#{uploadBean.file}"/>
<h:commandButton value="Upload!" action="#{uploadBean.upload}"/>
</h:form>
Whenever I try to upload a file, upload() is never called.
Maybe the answer is too obvious, but I'm using JSF 2.2.7 and I'm not sure why this simple example shouldn't work?