2

Possible Duplicate:
How to choose the right bean scope?

I'm newbie in JSF programming, and I need a clarification about bean scopes. I have already read all the questions about this argument, but is not so clear. I don't understand the request scope well. I understand that: "This is the default scope and basically the bean is alive throughout a single HTTP request."

So for example, suppose that we ask the browser to open a web page with a form. When we make the request, a request scope bean is created, the life-cycle begins and after the render response phase, the Java bean is destroyed. Then we fill out the form and we press a button. This will start another HTTP request, right?

In the same context, if a have a view scope bean instead of a request scope bean, what is the difference? How many bean instances are created? Why is it better to use this with a datatable?

Community
  • 1
  • 1
Giorgio
  • 1,073
  • 3
  • 15
  • 33
  • Because I read on "jSF 2.0 the complete reference", that a request scope bean can survive a navigation to another page,provided it was during the same HTTP request? But after that we navigate to another page, the will be destroy( I suppose). How is possible? – Giorgio Nov 26 '12 at 13:29
  • You don't need to include signature in your post - your user card is added automatically. Read [FAQ](http://stackoverflow.com/faq#signatures) for more details. – Artemix Nov 26 '12 at 13:33
  • ok ok, thanks,i will remember...it's like an habit – Giorgio Nov 26 '12 at 13:41

1 Answers1

2

The request scope as all your sources including the post linked by BalusC say starts living a short while after your request hits the server, and is destroyed shortly after the last bit of the response has been send back.

Indeed, if you postback a form a new request starts and thus a new request scope. This means everything that is request scoped will be created again. So for a form that is first rendered, and then posted back once, 2 request scoped beans will be created.

The view scope lives as long as you do postbacks to the same view (page). This works by means of the hidden form parameter called javax.faces.ViewState. The value of this is an entry into some kind of logical Map if you use save state on server. How a JSF implementation actually resolves this is not that important here (but yes, it's mostly just a Map).

After the postback JSF is able to retrieve the exact same view scoped beans again by means of this parameter. So for a form that is first rendered, and then posted back once, 1 view scoped bean will be created.

For a datatable you will almost always want to use the view scope. The reason is that you want the data to be the same before and after a postback. If your data is 100% static and/or you don't have postbacks (your table is not in a form), you can use the request scope instead.

dexter meyers
  • 2,798
  • 2
  • 18
  • 22
  • Many Thanks @dexter, I think now I understand the difference, and I will manage better with the bean's scope. – Giorgio Nov 26 '12 at 17:27