How and when is a @ViewScoped bean destroyed in JSF?

It will be destroyed when a postback with a non-null outcome is been performed, or, the number of (logical) views in session has exceeded and the particular view is the first one in LRU chain (in Mojarra, that’s configureable by com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews context parameters, each with a default value of 15), or, the number … Read more

How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1

If you can upgrade to JSF 2.2, immediately do it. It offers a native @ViewScoped annotation for CDI. import javax.faces.view.ViewScoped; import javax.inject.Named; @Named @ViewScoped public class Bean implements Serializable { // … } Alternatively, install OmniFaces which brings its own CDI compatible @ViewScoped, including a working @PreDestroy (which is broken on JSF @ViewScoped). import javax.inject.Named; … Read more

@ViewScoped bean recreated on every postback request when using JSF 2.2

This, import javax.faces.view.ViewScoped; is the JSF 2.2-introduced CDI-specific annotation, intented to be used in combination with CDI-specific bean management annotation @Named. However, you’re using the JSF-specific bean management annotation @ManagedBean. import javax.faces.bean.ManagedBean; You should then be using any of the scopes provided by the very same javax.faces.bean package instead. The right @ViewScoped is over there: … Read more

Pass an object between @ViewScoped beans without using GET params

Depends on whether you’re sending a redirect or merely navigating. If you’re sending a redirect, then put it in the flash scope: Faces.setFlashAttribute(“car”, car); This is available in the @PostConstruct of the next bean as: Car car = Faces.getFlashAttribute(“car”); Or, if you’re merely navigating, then put it in the request scope: Faces.setRequestAttribute(“car”, car); This is … Read more

@ViewScoped calls @PostConstruct on every postback request

In other words, your @ViewScoped bean behaves like a @RequestScoped bean. It’s been recreated from scratch on every postback request. There are many possible causes for this, most of which boils down that the associated JSF view is not available anymore in the JSF state which in turn is by default associated with the HTTP … Read more