Rectangle 27 0

jsf PrimeFaces simple fileUpload: NullPointerException?


<context-param>
   <param-name>primefaces.UPLOADER</param-name>
   <param-value>commons</param-value>
</context-param>
<h:form enctype="multipart/form-data">        
    <p:growl id="messages" showDetail="true" />

    <p:fileUpload value="#{fileUploadView.file}" mode="simple" />

    <br /> <br />

    <h:commandButton value="Dummy Action" 
                     action="#{fileUploadView.upload}">
    </h:commandButton>
</h:form>
@ManagedBean
@RequestScoped
public class FileUploadView {

    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void upload() {
        System.out.println("Uploaded File Name Is :: " + 
                file.getFileName() + 
                " :: Uploaded File Size :: " + file.getSize());
        if(file != null) {
            FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

}
  • auto: This is the default mode and PrimeFaces tries to detect the best method by checking the runtime environment, if JSF runtime is at least 2.2 native uploader is selected, otherwise commons.
  • commons: This option chooses commons fileupload regardless of the environment, advantage of this option is that it works even on a Servlet 2.5 environment.
  • native: Native mode uses servlet 3.x Part API to upload the files and if JSF runtime is less than 2.2 and exception is being thrown.

You should choose only one option not paste all those three options. If you have commons-fileupload library in your project, I suggest you to choose commons.

in web.xml

Note