p:fileUpload 不会在支持 bean 中设置上传的文件 [英] p:fileUpload does not set uploaded file in backing bean
问题描述
我遇到了 PrimeFaces 的
问题.我创建了一个 Facelet 页面来上传文件,如下所示:
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>
还有一个支持 bean,如下所示:
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);
}
}
当我点击 Submit
按钮时,方法 submit()
被调用,但结果如下:
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?
推荐答案
请阅读PrimeFaces 用户指南.
Please read the <p:fileUpload>
chapter of the PrimeFaces User Guide.
首先要做的是配置解析多部分请求的文件上传过滤器.FileUpload 过滤器应该映射到 Faces Servlet.
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>
简单的文件上传
简单文件上传模式在遗留模式下工作,文件输入的值应该是UploadedFile
实例.
<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
}
请注意表单的 enctype="multipart/form-data"
属性.这对于 HTML 是强制性的,以便能够将文件发送到服务器.为了从 multipart/form-data
请求中提取数据,JSF 必须使用过滤器.如果没有它们,要么不会调用命令操作,要么所有属性都为 null
.
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 不会在支持 bean 中设置上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!