我正在尝试使用<p:fileUpload>
上传文件。以下是观点:
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload
fileUploadListener="#{viewscopedBean.handleFileUpload}"
mode="advanced" dragDropSupport="true" multiple="true"
update="messages" />
</h:form>
豆:
@ManagedBean
@ViewScoped
public class ViewscopedBean implements Serializable{
private List<UploadedFile> uploadedFiles; //to remember which files have been uploaded
public ViewscopedBean() {
super();
System.out.println("@constructor");
uploadedFiles = new ArrayList<UploadedFile>();
}
public void handleFileUpload(FileUploadEvent event) {
System.out.println("! HANDLE FILE UPLOAD !");
// do something
}
public List<UploadedFile> getUploadedFiles() {
return uploadedFiles;
}
public void setUploadedFiles(List<UploadedFile> uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}
}
web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>512000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
当我点击upload button
时,进度条最多填充100%(就像它做了一些事情),然后重新加载页面(为每个上传的文件调用构造函数) - {{1永远不会调用方法。
没有任何错误或警告,它只是没有做它应该做的事情。我有JSF 2.0并使用Primefaces 4,可能有问题吗?
这是如何引起的?如何解决?
答案 0 :(得分:2)
首先,确保您的类路径上有commons-fileupload
及其编译时依赖项commons-io
。未被调用的处理程序表明您缺少这些依赖项。
自4.0
以来,现在有一个可选 context-param,它指定用于处理FileUpload上传的服务器端引擎:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
如果没有context-param
,PrimeFaces会通过检测选择最合适的上传引擎。
鉴于您不使用JSF 2.2
,这意味着它将首先选择自动,因此将重定向以使用 commons < /强>
因此,您可以了解项目中commons-fileupload
和commons-io
的重要性。
当我将代码复制并粘贴到包含这些依赖项的Primefaces 3.5
项目时,您的代码对我有用。构造函数只调用一次,每次上传文件时都会调用handle方法。
我使用4.0
重复测试,然后再次调用构造函数一次。
以下是我在web.xml
中使用的配置(commons-fileupload
所需):
<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>
这是支持bean:
package uk;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
@ManagedBean(name = "tempBean")
@ViewScoped
public class TempBean {
private List<UploadedFile> uploadedFiles; //to remember which files have been uploaded
public TempBean() {
super();
System.out.println("@constructor");
uploadedFiles = new ArrayList<UploadedFile>();
}
public void handleFileUpload(FileUploadEvent event) {
System.out.println("! HANDLE FILE UPLOAD !");
// do something
}
public List<UploadedFile> getUploadedFiles() {
return uploadedFiles;
}
public void setUploadedFiles(List<UploadedFile> uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}
}
这是页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:body>
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload
fileUploadListener="#{tempBean.handleFileUpload}"
mode="advanced" dragDropSupport="true" multiple="true"
update="messages" />
</h:form>
</h:body>
</html>
最后,我的POM片段包含Apache Commons的依赖项和版本:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>