简体   繁体   中英

When does a JSF 2.0 ViewScoped Bean die?

I would like to have a Ajax-Based Tabbed interface in my website.

I would also like to have a bean for each of the tabs.
These beans are expected to be " born " when entering the tab and " die " when leaving the tab.

Important - The switch between tabs must be an AJAX event and not a full page redraw.

I thought @ViewScoped is the appropriate scope for this type of behaviour but I am stuck on the issue of killing the bean when leaving a tab. To my best knowledge, a @ViewScoped bean will only die on a redirect/navigation event.

Is there a proper way to make the @ViewScoped bean die? Should I use a different scope?

Thanks!

UPDATE

Reading BalusC's answer on this question is good indication:

A view scoped bean lives as long as you interact with the same view (ie you return void or null in bean action method). When you navigate away to another view, eg by clicking a link or by returning a different action outcome, then the view scoped bean will be trashed by end of render response and not be available in the next request.

So, according to this, I could return a different outcome from an action method and make the @ViewScoped bean die.
But to do this I have to disable navigation after an outcome from an action method (JSF 2 implicit navigation) and I don't know how to do that (or if that's the right way to achieve my goal)

I would use just one ViewScoped bean having the data to show as a simple POJO property. The pages will be lazy intialized.

@ManagedBean
@ViewScoped
public class BackingBean {

    private WizardData wizardData;

    public WizardData getWizardData() {
        if (wizardData == null) {
            wizardData = new WizardData();
        }
        return wizardData;
    }

    public void setWizardData(WizardData wizardData) {
        this.wizardData = wizardData;
    }
}

public class WizardData {
    private WizardPage1 page1;
    private WizardPage2 page2;

    public WizardPage1 getPage1() {
        if (page1 == null) {
            page1 = new WizardPage1();
        }
        return page1;
    }

    public void setPage1(WizardPage1 page1) {
        this.page1 = page1;
    }

    public WizardPage2 getPage2() {
        if (page2 == null) {
            page2 = new WizardPage2();
        }
        return page2;
    }

    public void setPage2(WizardPage2 page2) {
        this.page2 = page2;
    }
}

So on pages the pojos will be created when you use them, eg

<h:outputText value="#{backingBean.wizardData.page1.someTextToShow}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM