I tried to use p:fileUpload but fileUploadListener method wasn't called, nor any exception generated. Without PrettyFaces, file upload works fine.
After some research, I found that Rewrite FAQ (http://ocpsoft.org/rewrite/docs/faq) answer for the question "Why are file uploads not working correctly any more?" could be the solution, but tested only on Tomcat. I tried that on GlassFish but without success.
Then I found a post in OcpSoft Forum that solves my problem: http://ocpsoft.org/support/topic/pretty-primefaces-fileupload/page/2/#post-25781. Just put the file upload code in other page source and reference that with a html iframe tag.
Ok, problem solved, but I'm not satisfied with iframe solution, whereas fileUploadListener method should be called without workaround. So, anyone knows how to configure in a way that file upload works with PrettyFaces?
Maven dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>
Xhtml source code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadMB.handleFileUpload}" auto="true"
update="fileName" />
<h:outputText id="fileName" value="#{fileUploadMB.uploadedFileName}" />
</h:form>
</h:body>
</html>
Managed bean:
@ManagedBean
@ViewScoped
@URLMapping(pattern = "/index", viewId = "/index.xhtml")
public class FileUploadMB {
private UploadedFile uploadedFile;
private String uploadedFileName;
public void handleFileUpload(FileUploadEvent evt) {
this.uploadedFile = evt.getFile();
this.uploadedFileName = evt.getFile().getFileName();
}
public String getUploadedFileName() {
return uploadedFileName;
}
public void setUploadedFileName(String uploadedFileName) {
this.uploadedFileName = uploadedFileName;
}
}
And web.xml:
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>
<param-value>
<!-- Packages with @URLMapping annotation from PrettyFaces -->
br.nti.ufms
</param-value>
</context-param>
<context-param>
<param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<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>
<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>*.xhtml</url-pattern>
</servlet-mapping>
This is probably a filter chain problem. Try adding the following on your register filters:
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
See more in prettyfaces faq question 2.