0

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.

Diego Nieto
  • 581
  • 2
  • 10
  • 23

3 Answers3

0

Try implementing followings,

add this method to your bean,

    public void handleProfileFileUpload(FileUploadEvent event) {
    if (event != null) {


        try {
            InputStream inputStream = event.getFile().getInputstream();
            // Set inputstream to your object
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Remove CommandButton and modify fileUpload config similar to this, fileUploadListener handle the uploading

<p:fileUpload fileUploadListener="#{<your bean>.handleProfileFileUpload}"
                            mode="advanced" sizeLimit="2097152"
                            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
SAP
  • 468
  • 2
  • 14
0

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>
Community
  • 1
  • 1
Diego Nieto
  • 581
  • 2
  • 10
  • 23
0

If you are using Spring MVC, your filter mapping should point to the Spring DispatcherServlet instead of Faces Servet.

<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>Spring MVC Servlet</servlet-name>
</filter-mapping>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Federico Traiman
  • 1,191
  • 1
  • 13
  • 18
  • Spring MVC you mean? OP is not using that. Afaik, using JSF AND MVC at the same time is a bad choice (if working at all, see http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc) – Kukeltje Aug 11 '16 at 14:02
  • I'm using Spring MVC / Webflow alongside Primefaces and JPA without problems. I don't believe that is a bad choice What do you mean when you say "OP is not using that"? – Federico Traiman Aug 11 '16 at 14:07
  • Are you using Spring MVC or Spring Webflow? Two different technologies if I read the link I posted carefully. You can use Spring Webflow with JSF, but not Spring MVC (although you can run them side by side if I read it correctly). So if you actually do more than this, I'd be very interested if there is some code publicly accessible to see how it is actually used and to maybe correct the link I posted – Kukeltje Aug 11 '16 at 14:24