0

I'm having troubles with Primefaces FileUpload

I'm running a Glassfish 4.1 server with Primefaces 5.2 and PrettyFaces 2.0.12.Final.

I'm trying to get a FileUpload to work, but all attempts fail. The Primefaces guide tells me the primefaces.UPLOADER param is not required and if I'm using JSF 2.2, it uses the native uploader. My faces-config.xml tells me I'm running JSF 2.2.

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              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-facesconfig_2_2.xsd">
    <application>
        <resource-bundle>
            <base-name>messages</base-name>
            <var>rmsg</var>
        </resource-bundle>
    </application>
</faces-config>

Then the guide tells me, that if I select the common primefaces.UPLOADER, I need the filter-mapping. But since, I'm running JSF 2.2, I don't think I need that part. (Although I've tried it, without success).

Next the html config:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/templates/main_template.xhtml">
    <ui:define name="top">
        <ui:decorate template="/WEB-INF/templates/top_menu.xhtml">
            <ui:param name="active" value="messaging" />
        </ui:decorate>

        <ui:decorate template="/WEB-INF/templates/sub_menu.xhtml">
            <ui:param name="active" value="OUTBOX" />
            <ui:param name="buttons" value="/messaging/archive,ARCHIVE;/messaging/inbox,INBOX;/messaging/outbox,OUTBOX" />
        </ui:decorate>
    </ui:define>

    <ui:define name="content">
        <h:form enctype="multipart/form-data">
            <p:fileUpload fileUploadListener="#{outboxBean.handleFileUpload}" />
        </h:form>
    </ui:define>
</ui:composition>

This is the complete page, and in the main_template is no wrapping tag. So no nested form tags.

The outboxBean is defined below:

package com.erates.messagingapp.messaging;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Erates
 */
@Named(value = "outboxBean")
@SessionScoped
public class OutboxBean implements Serializable {

    private static final long serialVersionUID = 1L;

    public void handleFileUpload(FileUploadEvent event) {
        Logger.getLogger(OutboxBean.class.getName()).log(Level.WARNING, "inside handleFileUpload");
    }

}

My web.xml looks like this:

<?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>none</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>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>480</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <display-name>outbox</display-name>
        <web-resource-collection>
            <web-resource-name>outbox</web-resource-name>
            <description/>
            <url-pattern>/outbox</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>MessagingUsers</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
        <description>Default user role</description>
        <role-name>MessagingUsers</role-name>
    </security-role>
</web-app>

Because I'm using Prettyfaces, I've added the webapp/META-INF/context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Context allowCasualMultipartParsing="true">
</Context>

I can't see what I'm doing wrong, but the handleFileUpload method is just not called. I've tried the simple uploader, without the multipart/form-data enctype on the form. Nothing works.

No error is thrown, not in the backing bean, nor the console. The file(s) is(are) just uploaded (I think so, because the files listed on the page disappear) without calling the listener.

What am I doing wrong here?

Erates
  • 646
  • 1
  • 9
  • 24
  • Why did you add the filter? Where's the debug information as mentioned in the duplicate? – BalusC Jul 08 '15 at 10:20
  • Well, the filter, I removed already but still doesn't work. Just haven't updated the question yet. Now I've removed the filter and also added the webapp/META-INF/context.xml file with the allowCasualMultipartParsing attribute enabled. Still doesn't work. The debug information, how do I debug a compiled imported jar? – Erates Jul 08 '15 at 11:09
  • Attach source code file. – BalusC Jul 08 '15 at 11:22
  • The FileUploadRenderer#decode() isn't even called. Just a reminder, I'm using Prettyfaces – Erates Jul 08 '15 at 11:41
  • OK, when adding the filter anyway, did you make sure `primefaces.UPLOADER` is explicitly set to `commons`? – BalusC Jul 08 '15 at 12:03
  • I did not. Now I have and it works. While I was debugging I saw a property of the FileUploadFilter was set to true, while it must be set to false to go through with the multipart upload. Found it at the same moment as you commented. Thanks!!! – Erates Jul 08 '15 at 12:17

0 Answers0