FileUpload does not fire event in PrimeFaces 3.5 using JSF 2.2

I cannot make the fileUpload component on PrimeFaces 3.5 to fire an event. I read a lot of posts about this topic and followed the advice, but it still won't work. I tried all modes (simple, automatic, advanced) without success.

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

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

Output from a managed bean using a method that is called from a 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 starts correctly after I checked it with the debugger tool in Chrome.

I added the necessary libraries to the classpath:

 commons-fileupload-1.3.jar commons-io-2.4.jar 
+6
source share
1 answer

This is caused by the FacesServlet JSF 2.2 change. Starting with this version, FacesServlet initially supports downloading files (in particular: multipart/form-data HTTP requests) thanks to the new @MultipartConfig annotation installed in Servlet 3.0. In addition, a new <h:inputFile> component has been added, offering a component for loading files into the standard set of JSF components.

All of this conflicts with the loading of PrimeFaces files in older versions of PrimeFaces 3.x, which do not account for this new JSF 2.2 feature at all. The PrimeFaces 3.x file upload filter analyzed and used the entire request, while it should leave this job until FacesServlet . This led to FacesServlet not being able to properly decode the HTTP request (by defining the values ​​and actions presented).

PrimeFaces 4.0, specifically designed for JSF 2.2, takes this into account. In this set of changes to the PrimeFaces file upload filter, you can see the changes made to recognize JSF 2.2 and bypass the parsing in the filter. Theoretically, it is also sufficient to completely remove the registration of the file upload filter from web.xml so that it is no longer used.

It should work fine if you upgrade to PrimeFaces 4.0. It was mistakenly officially released just 2 days ago, so you're pretty on time for this.

+6
source

Source: https://habr.com/ru/post/955247/


All Articles