Question

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>
Was it helpful?

Solution

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.

OTHER TIPS

It wasn't working for me either with Primefaces 5.0 & Prettyfaces Rewrite 2.0.12-final on Tomcat 7.0.41.

But After I removed this part from my web.xml

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
        org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>51200000</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/uploadedFilesDirectoryForGlassfish</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

and added to context.xml this:

<Context allowCasualMultipartParsing="true">

p:fileUpload started working all fine from the rewritten urls as well.

This page might be useful.

Also see: http://www.ocpsoft.org/rewrite/docs/faq & this SO answer


But Wait,

Just observed it was working for small file uploads only, for large file uploads fileUpload listener is still not triggered. Anybody with solutions ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top