[英]p:fileUpload calls bean constructor for each file
I'm trying to use <p:fileUpload>
to upload a file. 我正在尝试使用<p:fileUpload>
上传文件。 Here's the view: 这是视图:
<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>
Bean: 豆角,扁豆:
@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>
When I click on the upload button
, the progressbar is filling up to 100% (like it does something), but then the page is reloaded (constructor is being called for every uploaded file) - the handleFileUpload
method is never called. 当我单击上upload button
,进度条将占满100%(就像它所做的一样),但是然后重新加载页面(每个上载的文件都被调用构造函数)-从不调用handleFileUpload
方法。
There aren't any errors or warnings, it just doesn't do what it should. 没有任何错误或警告,只是没有执行应有的操作。 I have JSF 2.0 and use Primefaces 4, maybe there is a problem? 我有JSF 2.0并使用了Primefaces 4,也许有问题?
How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?
First things first, ensure that you have commons-fileupload
and its compile-time dependency commons-io
on your classpath. 首先,请确保您在类路径上具有commons-fileupload
及其编译时依赖项commons-io
。 The handler not being called suggests that you have these dependencies missing. 没有被调用的处理程序表明您缺少这些依赖项。
Since 4.0
, there is now an optional context-param which specifies the server-side engine for handling FileUpload uploads: 从4.0
,现在有一个可选的 context-param,它指定用于处理FileUpload上传的服务器端引擎:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
In the absence of the context-param
, PrimeFaces otherwise selects the most appropriate uploader engine by detection. 在没有context-param
的context-param
,PrimeFaces会通过检测选择最合适的上载器引擎。
Given that you are not using JSF 2.2
, this means that it will first select auto , and therefore will redirect to use commons . 鉴于您没有使用JSF 2.2
,这意味着它将首先选择auto ,因此将重定向到使用commons 。
So you can see how important it is that you have commons-fileupload
and commons-io
in your project. 因此,您可以看到在项目中拥有commons-fileupload
和commons-io
多么重要。
When I copied and pasted your code into a Primefaces 3.5
project containing these dependencies, your code worked for me. 当我将您的代码复制并粘贴到包含这些依赖项的Primefaces 3.5
项目中时,您的代码对我有用。 The constructor was called once and only once, and the handle method was called each time I uploaded a file. 构造函数仅被调用一次,并且每次我上传文件时都调用handle方法。
I repeated the test using 4.0
and again, the constructor was called once and only once. 我使用4.0
重复了测试,并再次调用了构造函数一次。
Here's the configuration that I used in web.xml
(needed by commons-fileupload
): 这是我在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>
Here's the backing bean: 这是支持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;
}
}
Here's the page: 这是页面:
<!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>
And finally here is the snippet of my POM that contains the dependencies and versions for Apache Commons: 最后,这是我的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.