I am quite confused with the view scope. I thought it could survive a JSF navigation to another page (no redirect, obviously), but it doesn't. So what's the advantage to use it instead of request scope, that if i summoned the same view it will be the same object?
-
[This answer](https://stackoverflow.com/questions/15265433/how-and-when-is-a-view-scope-bean-destroyed-in-jsf/15391453#15391453) to the question *"How and when is a view scope bean destroyed in JSF?"* might be relevant. – Lii Jan 20 '16 at 16:10
3 Answers
The advantage is that the bean survives postbacks to the same view. You don't need to preserve any data yourself anymore when used in rendered
attributes or as model for h:dataTable
or as hidden inputs, etcetera. In the past, a lot of hacks were been used to go around this.
A view scoped bean lives as long as you interact with the same view (i.e. you return void
or null
in bean action method). When you navigate away to another view, e.g. by clicking a link or by returning a different action outcome, then the view scoped bean will be trashed by end of render response and not be available in the next request.
See also:
-
great thanks Balus for answering i know why my freaking beans are reinstanciated my beans.... thanks!! – Necronet Jan 28 '11 at 19:30
-
@BalusC: Is there a time limit as to when the viewscoped bean will be destroyed if there is no interaction from the client side for a long time ? – Rajat Gupta Aug 07 '11 at 16:44
-
& another thing, if the user navigates to a link in another tab by clicking a link from current tab, keeping the current tab as well active, the ViewScoped bean will be destroyed right ? – Rajat Gupta Aug 07 '11 at 16:50
-
-
thanks and also if you could comment on my previous comment as well.. thanks ! – Rajat Gupta Aug 09 '11 at 17:36
-
6My understanding is that a View Scope is only destroyed if you use POST to navigate to a different page. If you type a new URL in the browser, click an anchor tag, or some other means of GET then the View Scope remains. Apparently there is a default queue of 15 views in Mojarra. Anyways this suggests that navigation in JSF should always use POST (with redirect GET) to avoid having View Scopes left hanging until they expire! – Ryan Sep 16 '11 at 14:48
-
@Raj - My understanding is that Views Scope map is tied to a Session and therefore will expire when the session expires. Since there is a LRU queue in Mojarra you are limited per session in how many views per session – Ryan Sep 16 '11 at 14:50
-
@Ryan: right, that depends on how you interpret it. The view scope is at least not accessible/useable anymore when you leave the view. That was my whole point. I don't want to make it unnecessarily complicated for the layman. – BalusC Sep 16 '11 at 14:52
-
Note: when I wrote POST, I mean postback to same page then redirect (not sure if that was clear). – Ryan Sep 16 '11 at 14:55
-
My understanding is that if you don't destroy the view scope by doing a post-redirect-get on the same page and the view hasn't expired then you can retrieve it from the browsers back button history for example – Ryan Sep 16 '11 at 14:58
-
@Ryan: on a proper dynamic web application browser cache would be disabled for dynamic pages. – BalusC Sep 16 '11 at 15:00
-
There aren't many websites today that don't have at least some dynamic content. Browsers wouldn't cache anything in the back button with your recommendation! I think it is okay to use the backbutton to view the state of a blog at the time you first retrieved it for example, even if that means you don't see that someone added a new comment. You just hit refresh if you suspect something changed. The point though is that technically the view may still linger on the server, but you are right in that it is still useful only to one page. I just wanted to elaborate, not invalidate your answer. – Ryan Sep 16 '11 at 15:33
Ripped straight from Core JavaServer Faces, 3rd Edition:
View Scope
View scope was added in JSF 2.0. A bean in view scope persists while the same JSF page is redisplayed. (The JSF specification uses the term view for a JSF page.) As soon as the user navigates to a different page, the bean goes out of scope.
If you have a page that keeps getting redisplayed, then you can put the beans that hold the data for this page into view scope, thereby reducing the size of the session scope. This is particularly useful for Ajax applications.

- 354,903
- 100
- 647
- 710
Maybe you are looking for the FlowScoped bean:
The Faces Flows feature of JavaServer Faces technology allows you to create a set of pages with a scope, FlowScoped, that is greater than request scope but less than session scope. For example, you might want to create a series of pages for the checkout process in an online store. You could create a set of self-contained pages that could be transferred from one store to another as needed.

- 1
- 1

- 58
- 8