【问题标题】:Yet another difficulty with PrimeFaces 3.0.M4 file uploadingPrimeFaces 3.0.M4 文件上传的另一个困难
【发布时间】:2011-11-15 16:56:10
【问题描述】:

所以,这个问题非常普遍。我想在mode="simple" (showcase) 中使用p:fileUpload 将文件上传到服务器。论坛上有很多关于文件上传问题的问答,但我没有找到一个全面的。文件上传组件在/faces/second.xhtml:

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

我已将 FileUpload 过滤器添加到 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。根据我发现它必须工作的解决方案,但它没有。 thumbnailthumbnailUpload 执行期间是 null。 我正在使用 primefaces 3.0.M4,jBoss 作为 7.0.2.Final

提前致谢

已解决: 问题不在fileUpload 组件中,而是在作为主页一部分包含的页面表单中。我已将prependId="false" 添加到内部表单中,一切正常

【问题讨论】:

    标签: java jakarta-ee file-upload primefaces


    【解决方案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 作为参数的操作方法,这样更容易:

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

    更新:

    因为您在第二步中只有文件上传,您可以尝试这样的事情。

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

    【讨论】:

    • 看起来问题出在嵌套表单中。我不知道如何在另一个主表单中使用form enctype="multipart/form-data" 的文件上传。你有什么建议吗?
    • @nikagra 在此处发布(更新/编辑问题)您的整个 xhtml 页面,看看您是否真的需要“嵌套表单”...也许您可以关闭一个表单并打开另一个表单。
    • 所以,问题似乎出在表单和ui:include 上。我已经更新了问题
    • @nikagra 看来 fileUpload 应该在版本 3 中工作。看看这个已关闭的问题:code.google.com/p/primefaces/issues/detail?id=1891
    • 这个话题也可能有帮助:forum.primefaces.org/viewtopic.php?f=3&t=1546
    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2012-03-10
    • 1970-01-01
    • 2012-09-20
    • 2014-03-28
    相关资源
    最近更新 更多