2012-02-05 94 views
1

我正在使用PrimeFaces 3並嘗試上傳文件,但是當我調試文件時始終爲空。 下面你可以看到我的代碼。任何人都可以發現問題是什麼?PrimeFaces上傳文件不上傳

<h:form enctype="multipart/form-data"> 

     <p:fileUpload value="#{uploadFileMB.file}" mode="simple" /> 
     <p:commandButton value="Submit" ajax="false" action="#{uploadFileMB.submit()}"/> 
     <h:outputLabel value="#{uploadFileMB.text}" /> 

</h:form> 



import javax.enterprise.context.SessionScoped; 
    import javax.faces.bean.ManagedBean; 
    import javax.faces.bean.RequestScoped; 
    import org.primefaces.model.UploadedFile; 


    @ManagedBean 
    @SessionScoped 
    public class UploadFileMB { 
    UploadedFile file; 
    String text; 

     public String getText() { 
      return text; 
     } 

     public void setText(String text) { 
      this.text = text; 
     } 


     public UploadedFile getFile() { 
      return file; 
     } 

     public void setFile(UploadedFile file) { 
      this.file = file; 
     } 

     public void submit(){ 


     System.out.println("Trial "+file); 
     UploadedFile a=file; 
     if(file==null) 
      text="not uploaded"; 
     else 
      text=file.getFileName()+" uploaded"; 
       } 
     /** Creates a new instance of UploadFileMB */ 
     public UploadFileMB() { 
     } 
    } 



<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>Faces Servlet</servlet-name> 
</filter-mapping> 

而且在這兩個web.xml和faces.config 過濾器我已經嘗試了一些建議和調試了很多次,但我無法弄清楚。

這是我的臉,配置:

<?xml version='1.0' encoding='UTF-8'?> 

<!-- =========== FULL CONFIGURATION FILE ================================== --> 

<faces-config version="2.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
    > 
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId> commons-fileupload</artifactId> 
      <version>1.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId> commons-io</artifactId> 
      <version>2.1</version> 
     </dependency> 
<lifecycle> 
<phase-listener>security.SecurityFilter</phase-listener> 
</lifecycle> 


<filter> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
    <init-param> 
     <param-name>uploadDirectory</param-name> 
     <param-value>C:/home/vanessa/Desktop</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
</filter-mapping> 



</faces-config> 

回答

0

我覺得這兩個文件是在項目中缺少的; commons-fileupload和commons-io。如果你的項目是maven,你可以將它們添加到你的pom.xml中;

 <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId> commons-fileupload</artifactId> 
      <version>1.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId> commons-io</artifactId> 
      <version>2.1</version> 
     </dependency> 

否則請從http://commons.apache.org下載它們並添加您的庫。

+0

謝謝。但它不起作用。我沒有maven,所以我剛剛添加了上面寫的jar和代碼,但它仍然不起作用。其他建議?再次感謝! – Viola 2012-02-05 13:07:43

+0

你的託管bean應該實現Serializable,可能是它對你的錯誤 – Jman 2012-02-05 13:27:50

+0

謝謝。我添加了「實現可序列化」,但它仍然不起作用。任何其他IDE?我已經添加到我的問題我的面孔配置。 – Viola 2012-02-05 15:00:28

0

嗯,我看到你的代碼有三個錯誤,它可以解決你的問題,我不保證任何東西。

首先,您是從錯誤的包裝中導入@SessionScope,應該是javax.faces.bean.SessionScoped,另一個等級是CDI

其次,讓你的屬性在豆private我不確定它是否屬於這樣的屬性。這也是儘可能隱藏領域的好習慣。

第三,也是最重要的,將action更改爲actionListener並嘗試。如果仍然不起作用,請嘗試添加到您的方法參數ActionEvent event(並小心選擇合適的包裝,我曾經從javax.awt.導入ActionEvent並花費兩個小時計算出哪裏可能出現問題:-)