2014-12-18 116 views
4

我正在運行我的JSF項目,使用Spring Boot啓動它並利用整個Spring環境。配置是:Mojarra 2.2.8 + Primefaces 5.1 + Spring Boot 1.1.9。這是我的pom.xml文件看起來像:Primefaces FileUpload在Spring Boot中不起作用

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>project</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.1.9.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</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-web</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-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>${jsf.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>${jsf.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>5.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.primefaces.themes</groupId> 
      <artifactId>all-themes</artifactId> 
      <version>1.0.10</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.el</groupId> 
      <artifactId>el-api</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.web</groupId> 
      <artifactId>el-impl</artifactId> 
      <version>2.2</version> 
     </dependency> 
    </dependencies> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <start-class>com.tesicnor.workplace.monitor.Application</start-class> 
     <java.version>1.7</java.version> 
     <tomcat.version>7.0.57</tomcat.version> 
     <jsf.version>2.2.8</jsf.version> 
    </properties> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

,因爲它是上面規定的,我配置項目在Tomcat 7.0.57(Servlet的3.0兼容)啓動運行。所有JSF函數都正常工作,但問題是無法獲得Primefaces p:fileUpload組件的工作,無論是基本還是高級版本。文件上傳偵聽器不會被調用,也不會引發錯誤。

這是我的bean的代碼:

@ManagedBean 
@javax.faces.bean.ViewScoped 
public class Bean { 

    public void handleFileUpload(FileUploadEvent event) { 
     FacesMessage message = new FacesMessage("Succesful", event.getFile() 
       .getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, message); 
     System.out.println("Uploaded!"); 
    } 

} 

這就是我的XHTML文件看起來像,模板下:

<ui:composition template="/WEB-INF/template.xhtml" 
    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"> 

    <ui:define name="content"> 
     <h:form> 
      <p:fileUpload fileUploadListener="#{bean.handleFileUpload}" /> 
     </h:form> 
    </ui:define> 
</ui:composition> 

沒有什麼特別的代碼的話,那必須是服務器配置問題。當我放下文件並點擊上傳時,FacesServlet被擊中。所以請求被執行並且沒有顯示javascript錯誤。但是當我執行InvokeApplicationPhase類的調試時,我的方法應該被調用,我發現沒有要處理的事件。所以FileUploadEvent沒有被附加到循環。

而且,就是這樣正確地執行在Tomcat 7的文件上傳另一個項目和JSF版本的調試堆棧:

enter image description here

這裏NativeFileUploadDecoder被調用。然而,這並不是在我的Spring引導項目中發生的,並且沒有調用FileUpload#visitTree以下的方法。

我嘗試了其他選擇,發現當我使用一些<h:form enctype="multipart/form-data">時,沒有任何內部組件的操作方法被調用,即使我放置了一個普通的h:commandButton

+0

和上傳過濾器?我不確定是否使用了spring-boot,也許它有一些過濾器......當我使用rewrite url時,我遇到了同樣的問題,我必須添加到'PrimeFaces FileUpload Filter'的'filter-mapping'中' FORWARD'。這是一個瘋狂的猜測:) –

+0

感謝您的答案@HatemAlimam。我一直在瀏覽文檔,實際上PF在其最新版本中有兩個上傳選項,一個依賴於Servlet 3.0規範,另一個依賴於'apache-commons'庫。只有使用'apache-commons'時才需要過濾器。最後我以這種方式實現了它的工作,但與原生規格無關。不過要知道所有這些的根本原因是很有趣的。我會發佈一個小型演示項目,以便人們能夠重現該問題。 –

回答

4

最後,我使用Apache Commons庫進行了工作。同樣正是我們可能會在標準的web.xml文件做,這是我的上下文初始化:

@Bean 
public ServletContextInitializer initializer() { 
    return new ServletContextInitializer() { 
     @Override 
     public void onStartup(ServletContext servletContext) 
       throws ServletException { 
      servletContext.setInitParameter("primefaces.THEME", "bluesky"); 
      servletContext.setInitParameter(
        "javax.faces.FACELETS_SKIP_COMMENTS", "true"); 
      servletContext.setInitParameter(
        "com.sun.faces.expressionFactory", 
        "com.sun.el.ExpressionFactoryImpl"); 
      servletContext.setInitParameter("primefaces.UPLOADER", 
        "commons"); 
     } 
    }; 
} 

我明確地告訴Primefaces使用公共上傳,像文檔說,(其它選擇是使用原生,這是不工作)。

然後,只需添加這兩個依賴的項目,我們已經準備好了:

<dependency> 
    <groupId>commons-fileupload</groupId> 
    <artifactId>commons-fileupload</artifactId> 
    <version>1.3</version> 
</dependency> 
<dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.2</version> 
</dependency> 

我把線程打開設在本地模式中的某些反應。

參見:

+0

將這兩個答案都與PF5.3結合使用。謝謝!爲我節省了很多時間。 – SeC

+1

結合這兩個答案爲我工作以及PF 6.1春季啓動1.5.8。非常感謝我的妻子尋找解決方案的幾天。 –

4

這可能是還需要XML過濾器配置轉移到基於Java的配置:

<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
</filter-mapping> 

您@Configuration

@Bean 
public FilterRegistrationBean FileUploadFilter() { 
    FilterRegistrationBean registration = new FilterRegistrationBean(); 
    registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter()); 
    registration.setName("PrimeFaces FileUpload Filter"); 
    return registration; 
} 

與上面的回答結合它的工作對我來說

+0

它是否以本地方式工作?我的意思是,在你的java配置中有'servletContext.setInitParameter(「primefaces.UPLOADER」, 「native」);''預期此配置在沒有FileUpload過濾器的情況下工作。 –

4

只是把我的兩分錢,與春季啓動1.4.2.RELEASE,Primefaces 6.0,OCP軟改寫2.0.12.Final,JSF 2.1。 29-08和部署在Tomcat 8上的應用程序,我還需要禁用spring hiddenHttpMethodFilter。

@Bean 
public FilterRegistrationBean hiddenHttpMethodFilterDisabled(
     @Qualifier("hiddenHttpMethodFilter") HiddenHttpMethodFilter filter) { 
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter); 
    filterRegistrationBean.setEnabled(false); 
    return filterRegistrationBean; 
} 

我花了這個問題近兩天和最後一件事,我曾嘗試逐一禁用春天過濾器,所以我希望這會幫助別人。

+0

在使用@Xtreme Biker的解決方案後,上傳工作可以在我的IDE中運行應用程序(不管「pom.xml」中的「包裝」設置)。然而,作爲在tomcat中部署的「戰爭」,它並沒有奏效。添加您的解決方案也爲我做了。謝謝 – wiseOne

相關問題