@ViewScoped defines a bean whose lifetime matches a single JSF view: the bean is created when a user first opens a page and lives as long as the user stays on that view through postbacks and Ajax requests. It is destroyed when the user navigates to a different view or the session ends.
This makes it the right scope for page-level state such as form data, table selections or wizard steps, where @RequestScoped would lose the data after each request and @SessionScoped would keep it too long.
Two annotations share the name, and mixing them is a common source of bugs.
| Annotation | Package | Works with |
|---|---|---|
@ViewScoped | jakarta.faces.view.ViewScoped | CDI beans (Jakarta Faces 2.3 and newer, Jakarta EE 9 and later) |
@ViewScoped | javax.faces.bean.ViewScoped | Old JSF managed beans with @ManagedBean (deprecated, removed in Faces 4.0) |
For new code, use jakarta.faces.view.ViewScoped together with @Named. The older @ManagedBean model was deprecated in JSF 2.3 and removed in Jakarta Faces 4.0 (Jakarta EE 10). The javax package names changed to jakarta in Jakarta EE 9, so on Jakarta EE 9 or newer you import from jakarta.faces.
The page uses the bean through Expression Language:
Each Ajax request returns to the same TodoBean instance, so the list keeps growing. If you reload the page with a fresh GET request, JSF creates a new view and a new bean.
@PreDestroy methods run.View-scoped beans are stored in the user session, keyed by view. The number of stored views per session is limited by implementation settings, so old views of a user with many open tabs can be evicted.
| Scope | Lifetime | Typical use |
|---|---|---|
@RequestScoped | One HTTP request | Simple actions, read-only pages |
@ViewScoped | While the user stays on one view | Forms, Ajax pages, tables |
@ConversationScoped | Explicit begin and end | Multi-page wizards |
@SessionScoped | Whole user session | Login data, preferences |
@ApplicationScoped | Whole application | Shared caches, configuration |
Serializable, because it is stored in the session and may be passivated or replicated.EntityManager, open streams or FacesContext in fields. Inject a request-scoped or stateless service instead.javax.faces.bean annotation with @Named; the scope is silently ignored and the bean behaves like request scope.@PostConstruct for initialization instead of the constructor, since injection happens after construction.A request-scoped bean is recreated for every request, including every Ajax call. A view-scoped bean survives all requests of the same view.
Usually the wrong import is used, or the bean does not implement Serializable, or the page uses a GET redirect that creates a new view. Check that you use jakarta.faces.view.ViewScoped with @Named.
Not directly. Spring has no JSF view scope by default, so you register a custom scope or use a library that provides one.
Each tab gets its own view, so each tab has its own bean instance for the same page.
@ViewScoped keeps JSF bean state for as long as the user stays on one page, which suits forms and Ajax interactions. Use the jakarta.faces.view.ViewScoped annotation with CDI @Named, make the bean Serializable, keep it lightweight, and choose conversation or session scope when state must outlive the view.