JSF 2.0.3,servlet 2.5文件上传问题

时间:2012-06-02 03:03:44

标签: file-upload jsf-2

我正在使用JSF 2.0.3和Servlet 2.5。,tomahawk20(1.1.10)。我在其他文章中遵循了Balu C推荐的步骤。但是,当我提交buttom(选择文件后),我得到“UploadedFile”实例的空值。我在web.xml中检查了我的ExtensionsFilter配置,但没有运气。你能帮忙吗?

My Manages bean:(IN session Scope)
-------------------
private UploadedFile uploadedFile;

public UploadedFile getUploadedFile() {
   return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
   this.uploadedFile = uploadedFile;
}

public String submitStep2() {
    System.out.println("File type: " + uploadedFile.getContentType()); 
    // <---- NULL POINTER here, because uploadedFile instance is null-->
}

web.xml

<filter>
        <filter-name>ExtensionsFilter</filter-name>
        <filter-class>
            org.apache.myfaces.webapp.filter.ExtensionsFilter
        </filter-class>
        <init-param>
            <description>
            Set the size limit for uploaded files.
                Format: 10  - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g  - 1 GB
            </description>
            <param-name>uploadMaxFileSize</param-name>
            <param-value>10m</param-value>
        </init-param>
        <init-param>
            <description>
            Set the threshold size - files below this limit are stored 
            in memory, files above this limit are stored on disk.
                Format: 10  - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g  - 1 GB
            </description>
            <param-name>uploadThresholdSize</param-name>
            <param-value>10k</param-value>
        </init-param>
        <init-param>
            <description>
                Set the path where the intermediary files will be stored.
            </description>
            <param-name>uploadRepositoryPath</param-name>
            <param-value>/temp</param-value>
        </init-param>
    </filter>

<filter>
        <filter-name>ExtensionsFilter</filter-name>
        <filter-class>
            org.apache.myfaces.webapp.filter.ExtensionsFilter
        </filter-class>
        <init-param>
            <description>
            Set the size limit for uploaded files.
                Format: 10  - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g  - 1 GB
            </description>
            <param-name>uploadMaxFileSize</param-name>
            <param-value>10m</param-value>
        </init-param>
        <init-param>
            <description>
            Set the threshold size - files below this limit are stored 
            in memory, files above this limit are stored on disk.
                Format: 10  - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g  - 1 GB
            </description>
            <param-name>uploadThresholdSize</param-name>
            <param-value>10k</param-value>
        </init-param>
        <init-param>
            <description>
                Set the path where the intermediary files will be stored.
            </description>
            <param-name>uploadRepositoryPath</param-name>
            <param-value>/temp</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

<servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

<servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>


<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:t="http://myfaces.apache.org/tomahawk"
    xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
<f:view>

<h:head>
...
</h:head>
<h:body>
...
Choose File: 
<t:inputFileUpload id="file" value="#{createMB.uploadedFile}" required="true" />
                                <br />
<h:commandButton value="Submit" action="#{createMB.submitStep2}" />
....

1 个答案:

答案 0 :(得分:0)

最明显的是你忘了将属性enctype =“multipart / form-data”添加到h:form标签。

示例:

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

这当然只是猜测,因为您的代码段中未提供此部分。