Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSF 2 @ViewScoped doesn't call @PreDestroy method

865459May 30 2011 — edited May 31 2011
howdy guys!
i've been tested the jsf ViewScoped in my application but i have a doubt. The managed bean noted with @ViewScoped sometimes does not call the @PreDestroy method.
See example below

ViewScopeMB.java

@ManagedBean
@ViewScoped
public class ViewScopeMB implements Serializable {
private int counter;

public ViewScopeMB() { }

@PostConstruct
public void contruct() {
System.out.println("iniciando bean");
this.counter = 0;
}

// getters and setters

public void increment() {
System.out.println("incrementing...");
this.counter++;
}

public void forceSetView() {
System.out.println("modificando a viewroot");
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getApplication().getViewHandler().createView(context, "/faces/one.xhtml");
context.setViewRoot(root);
}

@PreDestroy
public void destroy() {
System.out.println("destroy the bean");
}


I'm using f:ajax to incrementing the counter and this work fine, the bean instance is kept, but when i navigate for new page using <a:href for exemple the ViewScopeMB instance is not destroyed. I change my link to <h:commandLink action="rule"... and the @PreDestroy method is called and the finalize method too.
My question is, this is a correct behavior of JSF 2? If the user use the navigator back button or to type the new url or the button with javascript history.go(-1) my instance of ViewScopeMB will never be destroyed in the user session?
When i used forceSetView() method to force a new viewRoot the @PreDestroy is called.
There are way to solve this case?
Sorry if i not been very clear.
Anybody help-me?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2011
Added on May 30 2011
2 comments
2,901 views