Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between View and Request scope in managed beans

Tags:

jsf

jsf-2

What does the view scope mean? Can anyone explain about it, so that I can understand how it differs from the request scope?

like image 509
Maddy Avatar asked May 17 '11 03:05

Maddy


People also ask

What scopes are available for a managed bean?

You can use following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application. Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.

What is request scoped bean?

A request-scoped bean is an object managed by Spring, for which the framework creates a new instance for every HTTP request. The app can use the instance only for the request that created it. Any new HTTP request (from the same or other clients) creates and uses a different instance of the same class (figure 2).

What is the difference between request scope and session scope?

In request scope, a bean is defined to an HTTP request whereas in session scope, it is scoped to an HTTP session. So for an instance, if the bean scope is request and, a user makes more than one request for a web page in his user session, then on every request a new bean would be created.


1 Answers

A @ViewScoped bean lives exactly as long as a JSF view. It usually starts with a fresh new GET request, or with a navigation action, and will then live as long as the enduser submits any POST form in the view to an action method which returns null or void (and thus navigates back to the same view). Once you refresh the page, or return a non-null string (even an empty string!) navigation outcome, then the view scope will end.

A @RequestScoped bean lives exactly as long a HTTP request. It will thus be garbaged by end of every request and recreated on every new request, hereby losing all changed properties.

A @ViewScoped bean is thus particularly more useful in rich Ajax-enabled views which needs to remember the (changed) view state across Ajax requests. A @RequestScoped one would be recreated on every Ajax request and thus fail to remember all changed view state. Note that a @ViewScoped bean does not share any data among different browser tabs/windows in the same session like as a @SessionScoped bean. Every view has its own unique @ViewScoped bean.

See also:

  • How to choose the right bean scope?
  • The benefits and pitfalls of @ViewScoped
like image 130
BalusC Avatar answered Oct 16 '22 19:10

BalusC