2011-11-15 22 views
1

因此,這個問題非常普遍。我想用p:fileUploadmode="simple"showcase)上傳文件到服務器。在論壇上有很多關於文件上傳問題的問題和答案,但我沒有找到全面的答案。文件上傳組件是/faces/second.xhtmlPrimeFaces 3.0.M4文件上傳的另一個難點

<h:form> 
    <p:wizard> 
     <p:tab id="firstStep" title="First step"> 
      <ui:include src="/faces/first.xhtml" /> 
     </p:tab> 
     <p:tab name="secondStep" title="Second step"> 
      <ui:include src="/faces/second.xhtml" /> 
     </p:tab> 
    </p:wizard> 
</h:form> 

其中/faces/second.xhtml包含:

<p:panel> 
    <h:panelGrid columns="2" id="file"> 
     <p:fileUpload value="#{tutorialBean.thumbnail}" mode="simple" /> 
     <p:commandButton value="Submit" update="growl" 
      actionListener="#{tutorialBean.thumbnailUpload}" /> 
    </h:panelGrid> 
</p:panel> 

豆:

@SessionScoped 
public class TutorialBean { 
    private UploadedFile thumbnail; 

    public void thumbnailUpload(){ 
    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, 
      "File uploaded", thumbnail.getFileName()); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 

    // setters and getters 
} 

我已經添加了文件上傳過濾器web.xml

<!-- PrimeFaces 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>100000</param-value> 
    </init-param> 
    <init-param> 
     <param-name>uploadDirectory</param-name> 
     <param-value>C:\Users\name\Desktop\</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
</filter-mapping> 

我已將commons-io(2.0.1)和commons-fileupload(1.2.2)添加到我的pom.xml。根據我的解決方案,我發現它必須工作,但事實並非如此。 thumbnail在執行thumbnailUpload期間爲null。 我使用primefaces 3.0.M4,JBoss作爲7.0.2.Final

在此先感謝

解決:這個問題是不是在fileUpload組成部分,但在頁面形式,被列爲部分的主頁面。我已經添加到prependId="false"內輪廓,一切工作正常

回答

1

與簡單/標準Primefaces過濾器首先嚐試:

<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> 

我真的不知道爲什麼它不工作,這個給你。我嘗試了簡單和高級模式,兩者都很好。此外,如果您使用高級模式下可以使用接收FileUploadEvent event作爲PARAM的操作方法,並更容易這樣:

String fileName = "Uploaded_"+event.getFile().getFileName(); 
     System.out.println("Uploaded: " + fileName); 

UPDATE:

因爲你只有在第二個步驟中的文件上傳,您可以嘗試這樣的事情。

<p:wizard> 
    <p:tab id="firstStep" title="First step"> 
     <h:form> 
     <ui:include src="/faces/first.xhtml" /> 
     </h:form> 
    </p:tab> 
    <p:tab name="secondStep" title="Second step"> 
     <p:panel> 
      <h:form multipart> 
      <h:panelGrid columns="2" id="file"> 
       <p:fileUpload value="#{tutorialBean.thumbnail}" mode="simple" /> 
       <p:commandButton value="Submit" update="growl" 
        actionListener="#{tutorialBean.thumbnailUpload}" /> 
      </h:panelGrid> 
      </h:form> 
     </p:panel> 
    </p:tab> 
</p:wizard> 
+0

它看起來像一個問題是在嵌套窗體。我不知道如何通過'form enctype =「multipart/form-data」'在另一個主窗體中使用文件上傳。你有什麼建議嗎? – nikagra

+0

@nikagra在這裏發佈(更新/編輯問題)你的整個xhtml頁面,看看你是否真的需要'嵌套窗體'...也許你可以關閉窗體並打開另一個窗體。 – spauny

+0

xhtml已更新 – nikagra