JSF 2 @ViewScoped doesn't call @PreDestroy method
865459May 30 2011 — edited May 31 2011howdy 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?