我想使用Primefaces fileupload,这是我的jsf页面:
<h:form id="uploadForm" enctype="multipart/form">
<p:fileUpload id="fileUpload" value="#{fileUploadController.file}" mode="simple"/>
<p:commandButton value="Submit" ajax="false"
actionListener="#{fileUploadController.upload}" />
</h:form>
这是我的豆子:
package FileUploadController;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@ManagedBean
@SessionScoped
public class FileUploadController {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
以下是prim所需的依赖项。 fileupload,pom.xml(我使用maven项目):
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>portlet-api</groupId>
<artifactId>portlet-api</artifactId>
<version>1.0</version>
</dependency>
这是我的web.xml:
<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
文件。当我单击“提交”按钮时,上传方法被正确调用,但UploadedFile
文件仍然为空,未设置为选择文件。我该如何设置此文件?
答案 0 :(得分:1)
enctype属性enctype="multipart/form"
无效。 enctype
属性指定用于将表单提交到服务器的内容类型。
根据PrimeFaces文档(第183页),enctype
属性应定义为enctype="multipart/form-data"
,在您的情况下:
<h:form id="uploadForm" enctype="multipart/form-data">
...
</h:form>