Question

Invoke PostConstruct method at Initial requests. But, when I upload the images, there are multiple call PreDestroy method. That's mean, view id of ImageActionBean has been change for each FileUploadEvent. As I thought ViewID does not changed before redirect to another page, I tried to clear the temp storage of uploaded files.

If I upload three images, invoke PreDestroy method fourth times. That's why, I get at lease one file.

My Enviroment

- 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

Stack Trace :

>>>>> 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
    }
}
Was it helpful?

Solution

The OmniFaces CDI @ViewScoped is designed to be used with CDI managed beans, not with JSF managed beans. The @ManagedBean creates a JSF managed bean, not a CDI managed bean. JSF managed bean facility doesn't support CDI managed bean scopes, but only JSF managed bean scopes. When no one is explicitly declared, then the default scope @RequestScoped will actually be used.

In effects, your bean is a request scoped bean and this totally explains the symptoms you're observing.

In order to utilize the OmniFaces CDI @ViewScoped the right way, make your bean a real CDI managed bean by replacing @ManagedBean by @Named.

@Named
@ViewScoped
public class ImageActionBean implements Serializable {

Unrelated to the concrete problem, starting instance variable names with an uppercase is totally against the Java naming conventions. You're effectively basically doing like this:

ImageActionBean ImageActionBean = new ImageActionBean();

This is absolutely not recommended. You should instead effectively be doing

ImageActionBean imageActionBean = ImageActionBean();

Alter the EL variables to #{imageActionBean} accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top