【发布时间】:2014-01-21 04:50:00
【问题描述】:
在Initial requests 调用PostConstruct 方法。但是,当我上传图片时,有多个调用PreDestroy 方法。
这意味着,ImageActionBean 的视图 ID 已针对每个 FileUploadEvent 进行了更改。正如我认为ViewID 在重定向到另一个页面之前没有改变,
我试图清除上传文件的临时存储空间。
如果我上传三张图片,第四次调用PreDestroy 方法。这就是为什么,我至少得到一个文件。
我的环境
- JBoss 7.1.1 Final
- primefaces-4.0-20130910.075046-7
- omnifaces-1.7.jar
- jboss-jsf-api_2.1_spec-2.0.5.Final.jar
堆栈跟踪:
>>>>> Initialization Finished
>>>>> Destroy Finished
>>>>> Destroy Finished
>>>>> Destroy Finished
>>>>> Destroy Finished
<h:form id="attachmentForm" enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{ImageActionBean.handleProposalAttachment}"
mode="advanced" multiple="true" sizeLimit="3000000" update="attachmentTable"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" id="proposalAttachment"/>
</h:form>
@ManagedBean(name = "ImageActionBean")
@ViewScoped <-- org.omnifaces.cdi.ViewScoped
public class ImageActionBean implements Serializable {
private List<String> fileList;
@PostConstruct
public void init() {
fileList = new ArrayList<String>();
System.out.println("Initialization Finished");
}
@PreDestroy
public void destory() {
// clear uploaded file from temp storage
System.out.println("Destroy Finished");
}
public List<String> getFileList() {
return fileList;
}
public void handleProposalAttachment(FileUploadEvent event) {
UploadedFile uploadedFile = event.getFile();
String fileName = uploadedFile.getFileName().replaceAll("\\s", "_");
fileList.add(fileName);
//save uploadedFile to temp storage
}
}
【问题讨论】:
标签: java jsf primefaces omnifaces