PostConstruct
메서드를 Initial requests
으로 호출하십시오. 그러나 이미지를 업로드 할 때 PreDestroy
메서드를 여러 번 호출합니다. 즉, ImageActionBean
의보기 ID가 각 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
}
}
'@ Named'라고 제안한대로 사용했습니다. 'PostConstruct' 메쏘드는 괜찮지 만, 다른 페이지를 탐색 할 때'PreDestroy' 메쏘드를 호출하지 않습니다. 심지어'h : commandLink'를 사용하여 탐색합니다. – CycDemo
나를 위해 (그리고 쇼케이스에서) 작동합니다. 환경에 관한 모든 세부 사항과 함께 SSCCE가 도움이 될 것입니다. – BalusC
예, 저에게는 도움이되었지만, CDI를 제 jsf 버전과 JBoss 7에 통합하기가 어렵습니다. Thz – CycDemo