0

我正在使用 JSF 2.0.3 和 Servlet 2.5., tomahawk20 (1.1.10)。我按照 Balu C 在其他文章中推荐的步骤进行操作。但是当我提交按钮时(选择文件后),我得到“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}" />
....
4

1 回答 1

0

最明显的是您忘记将属性 enctype="multipart/form-data" 添加到 h:form 标签。

例子:

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

这当然只是一个猜测,因为您的代码片段中没有提供这部分。

于 2012-06-02T06:21:50.247 回答