p:fileUpload不会在backing bean中设置上传的文件 [英] p:fileUpload does not set uploaded file in backing bean

查看:140
本文介绍了p:fileUpload不会在backing bean中设置上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了PrimeFaces的< p:fileUpload> 问题。我创建了一个Facelet页面来上传文件,如下所示:

 < h:form id =welcomeForm> 
< p:fileUpload value =#{fileUploadController.uploadedFile}mode =simple/>
< h:commandButton value =Submitaction =#{fileUploadController.submit}/>
< h:message for =welcomeForm/>
< / h:表格>

以下是一个支持bean:

  public class FileUploadController {

private UploadedFile uploadedFile;
$ b $ public FileUploadController(){
}
$ b $ public UploadedFile getUploadedFile(){
return uploadedFile;
}

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


public void submit(){
//从上传的文件中获取信息
System.out.println(Uploaded file name:+ uploadedFile );
}

}

当我点击 Submit 按钮,方法 submit()被调用,但结果如下:


INFO:上传的文件名:null


这是怎么造成的?我解决了这个问题? > PrimeFaces用户指南一章。


FileUpload快速入门



首先要做的是配置文件上传过滤器来解析多部分请求。 FileUpload过滤器应映射到Faces Servlet。

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



简单文件上传



简单文件上传模式在传统模式下工作,文件输入的值应该是
UploadedFile 实例。

 < h:form enctype =multipart / form-data> 
< p:fileUpload value =#{fileBean.file}mode =simple/>
< p:commandButton value =Submitajax =false/>
< / h:表格>






  import org.primefaces.model.UploadedFile; 

public class FileBean {
private UploadedFile file;
// getter-setter
}


请注意表单的 enctype =multipart / form-data属性。这对于HTML来说是必须的,以便能够将文件发送到服务器。为了从 multipart / form-data 请求中提取数据,JSF必须使用过滤器。如果没有任何一个,命令行动将不会被调用,或者所有的属性都是 null


I am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload a file as below:

<h:form id="welcomeForm">
    <p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
    <h:commandButton value="Submit" action="#{fileUploadController.submit}" />
    <h:message for="welcomeForm" />
</h:form> 

And a backing bean as below:

public class FileUploadController {   

    private UploadedFile uploadedFile;

    public FileUploadController() {
    }       

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

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

    public void submit() {    
        // Get information you from the uploaded file
        System.out.println("Uploaded file name : " + uploadedFile);
    }

} 

When I click the Submit button, the method submit() is called but it the result is as below :

INFO: Uploaded file name : null

How is this caused and how can I solve it?

解决方案

Please read the <p:fileUpload> chapter of the PrimeFaces User Guide.

Getting started with FileUpload

First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet.

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

Simple File Upload

Simple file upload mode works in legacy mode with a file input whose value should be an UploadedFile instance.

<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{fileBean.file}" mode="simple" />
    <p:commandButton value="Submit" ajax="false"/>
</h:form>


import org.primefaces.model.UploadedFile;

public class FileBean {
    private UploadedFile file;
    //getter-setter
}

Please note the enctype="multipart/form-data" attribute of the form. This is mandatory for HTML in order to be able to send files to the server. The filter is mandatory for JSF in order to extract the data from multipart/form-data requests. Without either of them, either the command action won't be invoked, or all properties will be null.

这篇关于p:fileUpload不会在backing bean中设置上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆