上传的Primefaces文件没有在辅助bean中设置文件对象

时间:2014-08-25 08:37:53

标签: jsf file-upload jsf-2 primefaces

我正在使用随Apache Commons Uploader上传的Primefaces 5.0文件。控件出现在网页上,我可以选择一个文件,但是当我单击一个按钮来操作它时,控制器中的文件对象尚未设置。

网页:

<h:form id="master">
  <p:panelGrid columns="2">
    <p:fileUpload value="#{controller.file}" mode="simple"/>
    <p:commandButton value="Upload" 
        ajax="false" actionListener="# {controller.uploadFile}" />
  </p:panelGrid>

控制器:

@ManagedBean
@ViewScoped
public class Controller implements Serializable {

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

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

public void uploadFile() {
    if(file != null) {
        //Doesn't get here because file is null
    }
}

的web.xml

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>common</param-value>
</context-param>

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

我尝试将文件上传器设置为&#34; auto&#34;在web.xml中,但后来我得到了多部分异常,我无法解决。

1 个答案:

答案 0 :(得分:1)

您错过了在enctype="multipart/form-data"中使用<h:form>。改变如下

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

What does enctype='multipart/form-data' mean?