UploadedFile为null,Primefaces

时间:2016-09-02 20:17:46

标签: jsf primefaces

单击commandButton时,UploadedFile为null。什么错了?  在表单标签中我插入代码:enctype =" multipart / form-data"

 <h:form  enctype="multipart/form-data">
     <p:fileUpload  mode="simple"  value="#{b_cargar_tbl.file}" />
         <p:commandButton actionListener="#{b_cargar_tbl.upload()}"  value="Send" ajax="false" />
 </h:form>

bean的代码是:

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

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

public void upload() {
    if(file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

web.xlm

  <context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>auto|native|commons</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>

1 个答案:

答案 0 :(得分:0)

此解决方案: 将属性fileUploadListener添加到p:fileUpload并使用一个参数监听器FileUploadEvent

在您的managedbean类方法中创建

示例:

<p:commandButton actionListener="#{b_cargar_tbl.upload()}"  value="Send" 
fileUploadListener="#{b_cargar_tbl.upload}" ajax="false" />
托管bean添加方法中的

public void upload(FileUploadEvent event) {
        System.err.println("event.getFile().getFileName() = " + event.getFile().getFileName());     
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }