Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces FileUpload event not firing - JSF 2.0

I have posted my question on the Primefaces forum but no one has responded so I figured I would try here.

I have been attempting to get fileUpload to work for some time now. I am currently running the RC2 build with mojarra 2.0.3 and Tomcat 7.

I have a dialog which will contain the fileUpload component like so.

<p:dialog id="uploadFileDialog" >
   <h:form id="uplaodFileForm" prependId="false" enctype="multipart/form-data">
       <p:fileUpload fileUploadListener="#{fileUploadController.uploadFile} auto="true"/>    
   </h:form>
</p:dialog>

The fileUploadController looks like this

public class FileUploadController {
    public void uploadFile(FileUploadEvent event) {
         byte[] file = event.getFile().getContents();

         System.out.println("MADE IT INTO FILE UPLOAD !!! ");
    }
}

For some reason when the file is uploaded it never triggers the fileUploadEvent and it never gets into the controller. The upload looks like its working, the flash part renders and gives the impression its doing something but no backing bean is ever being called. I can seem to figure out what I am doing wrong and I have read just about every post on uploading a file using primefaces. Anyone know what I am doing wrong?

like image 308
DesireToUpload Avatar asked Dec 08 '10 14:12

DesireToUpload


3 Answers

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream

PrimeFaces fileupload uses Apache Commons FileUpload under the covers which in turn has another dependency, the Apache Commons IO. Ensure that you've both JAR's in your /WEB-INF/lib.


Update: as per the comments, you also need to ensure that the upload filter is been declared in web.xml as per the users' guide:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

And you also need to ensure that there are no other filters before in the web.xml which may be reading the HttpServletRequest#getInputStream(), because it can be read only once.

like image 171
BalusC Avatar answered Nov 09 '22 10:11

BalusC


I have also experienced a similar problem. The fix for me (using a Maven project) was to add the following dependencies in the pom.xml file:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>

This is the equivalent of having the corresponding .jar files in your WEB-INF/lib, so try do that if this is not a Maven project.

like image 3
Sabin Chirila Avatar answered Nov 09 '22 10:11

Sabin Chirila


that is correct you must add

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

AND later this

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

aditionaly if your using maven add this dependencies

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>     
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-io</artifactId>
     <version>1.3.2</version>
</dependency>
<dependency>
     <groupId>portlet-api</groupId>
     <artifactId>portlet-api</artifactId>
     <version>1.0</version>
</dependency>
like image 3
Rodrigo Avatar answered Nov 09 '22 10:11

Rodrigo