Primefaces FileUpload is not working with PrettyFaces and Spring Boot

UI Components for JSF
Post Reply
devmsaleh
Posts: 50
Joined: 06 Aug 2012, 11:43

27 Jun 2017, 20:05

i am using `springboot 1.5.2.RELEASE` with `JSF 2.2.14` and `primefaces 6.1` and `prettyfaces 3.4.1.Final`
my pom file is as follows:

Code: Select all

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>org.springframework</groupId>
    	<artifactId>myapp</artifactId>
    	<version>1</version>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.2.RELEASE</version>
    	</parent>
    
    	<properties>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<packaging>war</packaging>
    
    	<dependencies>
    
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-actuator</artifactId>
    		</dependency>
    
    
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-jpa</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-security</artifactId>
    		</dependency>
    
    
    
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>com.sun.faces</groupId>
    			<artifactId>jsf-api</artifactId>
    			<version>2.2.14</version>
    		</dependency>
    
    		<dependency>
    			<groupId>com.sun.faces</groupId>
    			<artifactId>jsf-impl</artifactId>
    			<version>2.2.14</version>
    		</dependency>
    
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.primefaces</groupId>
    			<artifactId>primefaces</artifactId>
    			<version>6.1</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.ocpsoft.rewrite</groupId>
    			<artifactId>rewrite-servlet</artifactId>
    			<version>3.4.1.Final</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.ocpsoft.rewrite</groupId>
    			<artifactId>rewrite-config-prettyfaces</artifactId>
    			<version>3.4.1.Final</version>
    		</dependency>
    
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-tomcat</artifactId>
    			<scope>provided</scope>
    		</dependency>
    
    
    		<dependency>
    			<groupId>org.apache.tomcat.embed</groupId>
    			<artifactId>tomcat-embed-jasper</artifactId>
    			<scope>provided</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-jersey</artifactId>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/bouncycastle/bcprov-jdk15 -->
    		<dependency>
    			<groupId>bouncycastle</groupId>
    			<artifactId>bcprov-jdk15</artifactId>
    			<version>140</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    		<dependency>
    			<groupId>commons-codec</groupId>
    			<artifactId>commons-codec</artifactId>
    			<version>1.10</version>
    		</dependency> 
    		
    		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    		<dependency>
    		    <groupId>org.apache.commons</groupId>
    		    <artifactId>commons-lang3</artifactId>
    		    <version>3.5</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
    		<dependency>
    		    <groupId>javax.mail</groupId>
    		    <artifactId>mail</artifactId>
    		    <version>1.4.7</version>
    		</dependency>
    		
    		
    		<!-- https://mvnrepository.com/artifact/javax.jcr/jcr -->
    		<dependency>
    		    <groupId>javax.jcr</groupId>
    		    <artifactId>jcr</artifactId>
    		    <version>2.0</version>
    		    <scope>provided</scope>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.apache.jackrabbit/jackrabbit-core -->
    		<dependency>
    		    <groupId>org.apache.jackrabbit</groupId>
    		    <artifactId>jackrabbit-core</artifactId>
    		    <version>2.15.2</version>
    		</dependency>
    		
    		<!-- https://mvnrepository.com/artifact/org.apache.jackrabbit/jackrabbit-api -->
    		<dependency>
    		    <groupId>org.apache.jackrabbit</groupId>
    		    <artifactId>jackrabbit-api</artifactId>
    		    <version>2.15.2</version>
    		</dependency>
    		
    		<!-- https://mvnrepository.com/artifact/org.apache.jackrabbit/jackrabbit-jcr-commons -->
    		<dependency>
    		    <groupId>org.apache.jackrabbit</groupId>
    		    <artifactId>jackrabbit-jcr-commons</artifactId>
    		    <version>2.15.2</version>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/org.apache.jackrabbit/jackrabbit-jcr2dav -->
    		<dependency>
    		    <groupId>org.apache.jackrabbit</groupId>
    		    <artifactId>jackrabbit-jcr2dav</artifactId>
    		    <version>2.15.2</version>
    		    <scope>provided</scope>
    		</dependency>
    		
    		
    		<dependency>
    		    <groupId>org.apache.jackrabbit</groupId>
    		    <artifactId>jackrabbit-jcr-rmi</artifactId>
    		    <version>2.15.2</version>
    		</dependency>
    		
    		<dependency>
    		    <groupId>commons-validator</groupId>
    		    <artifactId>commons-validator</artifactId>
    		    <version>1.6</version>
    		</dependency>
    		
    		<dependency>
    		    <groupId>commons-fileupload</groupId>
    		    <artifactId>commons-fileupload</artifactId>
    		    <version>1.3.3</version>
    		</dependency>
    		<dependency>
    		    <groupId>commons-io</groupId>
    		    <artifactId>commons-io</artifactId>
    		    <version>2.5</version>
    		</dependency>
    
    
    	</dependencies>
    
    	<build>
    		<finalName>myapp</finalName>
    		<plugins>
    			<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> 
    				<configuration> <source>1.6</source> <target>1.6</target> </configuration> 
    				</plugin> -->
    				
    			<plugin>
    			   <groupId>org.apache.maven.plugins</groupId>
    			   <artifactId>maven-surefire-plugin</artifactId>
    			   <configuration>
    			      <argLine>-Dfile.encoding=UTF8</argLine>
    			   </configuration>
    			</plugin>
    
    		</plugins>
    	</build>
    
    	<repositories>
    		<repository>
    			<id>spring-releases</id>
    			<name>Spring Releases</name>
    			<url>https://repo.spring.io/libs-release</url>
    		</repository>
    		<repository>
    			<id>org.jboss.repository.releases</id>
    			<name>JBoss Maven Release Repository</name>
    			<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
    		</repository>
    
    
    	</repositories>
    
    	<pluginRepositories>
    		<pluginRepository>
    			<id>spring-releases</id>
    			<name>Spring Releases</name>
    			<url>https://repo.spring.io/libs-release</url>
    		</pluginRepository>
    	</pluginRepositories>
    
    </project>
my springboot configuration class is as follows:

Code: Select all

package com.myapp;
    
    import java.util.Collections;
    
    import javax.faces.webapp.FacesServlet;
    import javax.servlet.DispatcherType;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.config.CustomScopeConfigurer;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletContextInitializer;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.filter.HiddenHttpMethodFilter;
    
    import com.ocpsoft.pretty.faces.annotation.URLMapping;
    import com.ocpsoft.pretty.faces.annotation.URLMappings;
    
    @SpringBootApplication
    @URLMappings(mappings = { @URLMapping(id = "login", pattern = "/login", viewId = "/faces/public/login.xhtml")})
    public class Application extends SpringBootServletInitializer {
    
    	private static final Logger log = LoggerFactory.getLogger(Application.class);
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class);
    	}
    
    	@Override
    	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    		return application.sources(Application.class);
    	}
    
    	@Bean
    	public ServletContextInitializer servletContextCustomizer() {
    		return new ServletContextInitializer() {
    			@Override
    			public void onStartup(ServletContext servletContext) throws ServletException {
    				servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
    				servletContext.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", Boolean.TRUE.toString());
    				servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", Boolean.TRUE.toString());
    				servletContext.setInitParameter("primefaces.FONT_AWESOME", Boolean.TRUE.toString());
    				servletContext.setInitParameter("primefaces.UPLOADER", "commons");
    			}
    		};
    	}
    
    	@Bean
    	public static CustomScopeConfigurer customScopeConfigurer() {
    		CustomScopeConfigurer configurer = new CustomScopeConfigurer();
    		configurer.setScopes(Collections.<String, Object>singletonMap(FacesViewScope.NAME, new FacesViewScope()));
    		return configurer;
    	}
    
    	@Bean
    	public ServletRegistrationBean servletRegistrationBean() {
    		FacesServlet servlet = new FacesServlet();
    		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
    		return servletRegistrationBean;
    	}
    
    	@Bean
    	public FilterRegistrationBean FileUploadFilter() {
    		FilterRegistrationBean registration = new FilterRegistrationBean();
    		registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter());
    		registration.setName("PrimeFaces FileUpload Filter");
    		registration.addUrlPatterns("/*");
    		registration.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST);
    		registration.addServletRegistrationBeans(servletRegistrationBean());
    		registration.setOrder(2);
    		return registration;
    	}
    
    	@Bean
    	public FilterRegistrationBean hiddenHttpMethodFilterDisabled(
    			@Qualifier("hiddenHttpMethodFilter") HiddenHttpMethodFilter filter) {
    		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
    		filterRegistrationBean.setEnabled(false);
    		return filterRegistrationBean;
    	}
    
    }
the xhtml page for upload:

Code: Select all

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://xmlns.jcp.org/jsf/core"
          xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
        <h:body>
     
        	<ui:composition template="/template/commonLayout.xhtml">   
    
        		<ui:define name="content">    			
        			<h:form enctype="multipart/form-data">
        			   <p:commandButton value="test" action="#{testBean.test}"/>
        			   <p:fileUpload fileUploadListener="#{testBean.handleFileUpload}" mode="advanced" dragDropSupport="false"
                      multiple="true"  update="messages" />
                      <h:messages id="messages"></h:messages>
        			</h:form>			
        		</ui:define>
    
    		
    
        	</ui:composition>
    
        </h:body>
    
    </html>
the fileupload backing bean is as follows:

Code: Select all

package com.myapp.beans;
    
    import java.io.Serializable;
    
    import javax.annotation.PostConstruct;
    
    import org.primefaces.event.FileUploadEvent;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    
    @Component("testBean")
    @Scope("view")
    public class TestBean implements Serializable {
    
    	private static final long serialVersionUID = -3568561158943789169L;
    
    	private static final Logger log = LoggerFactory.getLogger(TestBean.class);
    
    	public void handleFileUpload(FileUploadEvent event) {
    		log.info("####### handleFileUpload,file: " + event.getFile().getFileName());
    	}	
    
    }
i am running on `tomcat 9.0.0.M21` and when choosing the file and clicking upload i get no client side errors and no server side errors and the file is not getting uploaded,
although when i turned on primefaces debugging it prints that the file was uploaded:

Code: Select all

 2017-06-27 07:32:04,926 DEBUG o.p.webapp.filter.FileUploadFilter - Parsing file upload request
    2017-06-27 07:32:04,969 DEBUG o.p.webapp.filter.FileUploadFilter - File upload request parsed succesfully, continuing with filter chain with a wrapped multipart request
but the code in the listener in the backing bean was not invoked.

i also tried the context.xml solution for the prettyfaces issue:

Code: Select all

 <?xml version="1.0" encoding="UTF-8"?>
    <Context allowCasualMultipartParsing="true">
    </Context>
and the problem is not solved.

please advise how to fix this.

jgaslain
Posts: 5
Joined: 16 Dec 2016, 19:17

01 Sep 2017, 12:15

Hi,

I have the same problem,

Could we have some help, please ?

Julien
Primefaces 6.0
JSF 2.1
Tomcat 7.0
Java 8

MarkWinton
Posts: 50
Joined: 18 Oct 2013, 23:40

01 Sep 2017, 15:01

Could it be that your PrettyFaces mapping is causing your ViewScoped bean to be recreated when the upload is performed?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 35 guests