Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UploadedFile is null? [duplicate]

I am developing a Java Web Application with JSF 2.2 and Primefaces. I would like the user can upload a single file; for this, I used Primefaces 6.0 but it is not working and I just found tutorials (and examples) with Primefaces 5.x and it doesn't work either with Primefaces 5.0.

I preffer to work with Primefaces 6.0 but if you can help me with a Primefaces 5.x version, it's okay.

My web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <filter>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <servlet-name>facesServlet</servlet-name>
    </filter-mapping>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

My bean code is

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

@ManagedBean
@RequestScoped
public class ZipBean{

    UploadedFile arch;

    public ZipBean() {
    }

    public void subir() {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            if (this.arch != null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "PERFECT!","PERFECT!"));
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "FILE NULL","FILE NULL"));
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public UploadedFile getArch() {
        return arch;
    }

    public void setArch(UploadedFile arch) {
        this.arch = arch;
    }

}

My xhtml is:

<?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://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:growl autoUpdate="true"/>
        <h:form enctype="multipart/form-data">
            <p:fileUpload mode="simple" value="#{zipBean.arch}" />
            <p:commandButton value="Upload" action="#{zipBean.subir}" ajax="false" />
        </h:form>
    </h:body>
</html>

When I upload a file, the p:grow show the message FILE IS NULL.

I have the next jar libraries:

  • commons-fileupload-1.3.1.jar
  • commons-io-2.4.jar

Thank you!

like image 739
Juan Camacho Avatar asked Jan 13 '17 18:01

Juan Camacho


1 Answers

Here is an example of how to upload a file using primefaces 5.0; and you do not need commons-fileupload-1.3.1.jar and commons-io-2.4.jar; and also you do not need to change web.xml , and declare growl inside h:form for more information see this How to upload file in primefaces

FileUploadView .java

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@ManagedBean
public class FileUploadView {
    private UploadedFile file;
    public UploadedFile getFile() {
        return file;
    }
    public void setFile(UploadedFile file) {
        this.file = file;
    }
    public void upload() {
        if(file.getSize() > 0) {
            FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
else{
    FacesMessage message = new FacesMessage("Not Succesful", "file is not uploaded");
            FacesContext.getCurrentInstance().addMessage(null, message);
}
    }
}

basic.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://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
<h:form enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />

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

    <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" />
</h:form>
  </h:body>
</html>
like image 50
ArgaPK Avatar answered Nov 19 '22 07:11

ArgaPK