Search code examples
springfile-uploadprimefacesweb.xmlprettyfaces

Primefaces <p:fileUpload isn't invoking the bean method


I'm trying to implement the primefaces file upload but isn't invoking the bean method, by the way I'm using spring framework and prettyfaces:

faces-config.xml:

<?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>label</base-name>
            <var>msg</var>
        </resource-bundle>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>log4j-config-location</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>co.com.core.commons.LogContextListener</listener-class>
    </listener>
    <!-- ############################################# -->
    <!-- # File upload                                      # -->
    <!-- ############################################# -->
    <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>2097152</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <!-- ############################################# -->
    <!-- # QUARTZ                                    # -->
    <!-- ############################################# -->
    <!-- listener>
        <listener-class>
            org.quartz.ee.servlet.QuartzInitializerListener
        </listener-class>
    </listener-->
    <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>
</web-app>

.xhtml code:

<h:form enctype="multipart/form-data" prependId="false">
                            <p:fileUpload mode="simple" value="#{templateController.file}" />
                            <p:commandButton value="Upload" actionListener="#{templateController.upload}" ajax="false" />
                        </h:form>

bean method:

public void upload() {  
    FacesMessage msg = new FacesMessage("Success! is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

I tried adding the FORWARD to the filter because I'm also using pretty faces, but still not working, thanks.


Solution

  • Well finally after a lot of readings I solved this, in my case the solution was to add a context-param (primefaces.UPLOADER) into the web.xml file (check this answer please), this file looks like this, also could be helpful to set the project stage to develop (commented in this file), this could give you some additional information related to the debug process:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- context-param>
                <param-name>javax.faces.PROJECT_STAGE</param-name>
                <param-value>Development</param-value>
        </context-param-->
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/application-context.xml</param-value>
        </context-param>
    
        <context-param>
            <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
            <param-value>.xhtml</param-value>
        </context-param>
    
        <context-param>
            <param-name>log4j-config-location</param-name>
            <param-value>WEB-INF/log4j.properties</param-value>
        </context-param>
        <listener>
            <listener-class>co.com.core.commons.LogContextListener</listener-class>
        </listener>
        <!-- ############################################# -->
        <!-- # File upload                                      # -->
        <!-- ############################################# -->
        <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>2097152</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>PrimeFaces FileUpload Filter</filter-name>
            <servlet-name>Faces Servlet</servlet-name>
            <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
    
        <context-param>
            <param-name>primefaces.UPLOADER</param-name>
            <param-value>commons</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>
    </web-app>