調用PostConstruct
方法在Initial requests
。但是,當我上傳圖片時,有多種方法請致電PreDestroy
。 這就是說,查看ID爲ImageActionBean
已被更改爲每個FileUploadEvent
。因爲我認爲ViewID
在重定向到另一頁之前沒有改變,所以我試圖清除上傳文件的臨時存儲。通過全方位多次調用PreDestroy方法ViewScoped
如果我上傳三張圖片,請第四次調用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
}
}
我用你的建議'@命名'。 'PostConstruct'方法可以,但是當我導航到另一個頁面時,它不會調用PreDestroy方法,即使我使用'h:commandLink'來導航。 – CycDemo
適用於我(和展示櫃)。 SSCCE以及關於環境的所有細節將會有所幫助。 – BalusC
是的,這對我很有幫助,但我很難將CDI與我的'jsf' verson和'JBoss 7'整合。太赫茲 – CycDemo