Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fileUpload doesn't fire event in PrimeFaces 3.5 with JSF 2.2

I can't make fileUpload component on PrimeFaces 3.5 to fire the event. I have read many posts about that topic and followed advise there but still it doesn't work. I tried all of the modes (simple, auto, advanced) with no success.

If I use standard inputFile tag from the JSF specification it works properly.

This is my web.xml file:

<?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>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>redmond</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>

<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>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/home.xhtml</welcome-file>
</welcome-file-list>  
</web-app>

And this is part of my view page with the fileUpload tag:

<h:form enctype="multipart/form-data"> 
   <p:dialog id="basicDialog" header="Add pictures" widgetVar="dlg1" >  
       <p:fileUpload fileUploadListener="#{galleryManagedBean.addPicturesToGallery}" multiple="true"/>
   </p:dialog> 
</h:form>

The extract from managed bean with the method that is called from the tag:

@Named(value = "galleryManagedBean")
@RequestScoped
public class GalleryManagedBean {
    public void addPicturesToGallery(FileUploadEvent event)
    {
        System.out.println("Triggered upload");
    }
}

Also I would like to add that the Http POST request is fired properly after I checked it using the debugger tool in Chrome.

I have added necessary libraries to the classpath i.e. :

commons-fileupload-1.3.jar
commons-io-2.4.jar 
like image 669
bajer83 Avatar asked Oct 04 '13 11:10

bajer83


1 Answers

This is caused by a change in FacesServlet of JSF 2.2. Since that version, FacesServlet natively supports file uploads (specifically: multipart/form-data HTTP requests) thanks to the presence of the new Servlet 3.0 specific @MultipartConfig annotation. Also, a new <h:inputFile> component was been introduced to offer a file upload component in the standard JSF component set.

This all conflicts with PrimeFaces file upload facility in older PrimeFaces 3.x versions which didn't take this new JSF 2.2 feature into account at all. The PrimeFaces 3.x file upload filter parsed and consumed the entire request while it should leave this job up to the FacesServlet. This caused the FacesServlet to be unable to properly decode the HTTP request (determining the submitted values and actions).

PrimeFaces 4.0, which is designed specifically for JSF 2.2, has taken this all into account. In this changeset of the PrimeFaces file upload filter you can see the changes done to recognize JSF 2.2 and bypass the parsing in the filter. Theoretically, it should also suffice to entirely remove the file upload filter registration from web.xml so that this isn't used anymore.

It should work fine if you upgrade to PrimeFaces 4.0. It has coincidentally been officially released just 2 days ago, so you're pretty on time for that. 

like image 168
BalusC Avatar answered Oct 18 '22 09:10

BalusC