-1

What I want is implemented below, where I will have three pages.

userList.xhtml: This will have a list of users in a dataTable. Onclicking the user, I will see the details of respective user in the next page (detailsOfUser.xhtml)

detailsOfUser.xhtml: This page will have the details of a user who was clicked on in the above page. On this page I will have an Edit button, on clicking which I will get same values in textfield in next page (editUserDetails.xhtml)

editUserDetails.xhtml: Textfields with previous page user details. On this page I will have Save button, on clicking which I will be redirect back to detailsOfUser.xhtml where I will see that respective user updated values.

How should I use bean and scopes for those beans?

Should I use one bean for all three pages or three beans with different scopes?

APerson
  • 8,140
  • 8
  • 35
  • 49
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 2
    Have you seen [How to choose the right bean scope?](http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope), [What managed bean scope should I use?](http://stackoverflow.com/questions/7153909/what-managed-bean-scope-should-i-use), [Scopes in JSF 2](http://stackoverflow.com/questions/9733405/scopes-in-jsf-2), etc.? – skuntsel Feb 28 '13 at 13:11
  • 1
    The typical rule of thumb: choose the narrowest scope possible. My understanding (short) is that: use `@RequestScoped` for simple input/output data (that wouldn't use ajax, like showing extra details), `@ViewScoped` for holding data while using ajax or doing postbacks to the current view (thus, doing modifications, etc.), therefore, avoiding unnecessary bean reinstantiation, `@SessionScoped` for holding data inherently belonging to the user (session), like the user himself, his preferences, his choices made on site (users he viewed, etc.). – skuntsel Feb 28 '13 at 13:28

3 Answers3

1

I will make one bean (possibly ViewScope) for userList.xhtml. If your application is small and you like simplicity you could consider making this SessionScope as well.

For detailsOfUser.xhtml and editUserDetails.xhtml I will use another bean, which would have to be ConversationScoped or if you have the option of using MyFaces CODI (which I highly recommend), you can give them ViewAccessScope which is like an automatic Conversation scope.

This would allow you to write it in the easy and correct way. I understand that you are sharing information between detailsOfUser.xhtml and editUserDetails.xhtml.

Extra information- the ViewScope is problematic and you may need to have Seam or MyFaces CODI anyway to get it working.

bjedrzejewski
  • 2,378
  • 2
  • 25
  • 46
0

i would prefer to use Viewscope or even Requestscope, because you don't need the data for a longer time. You only need it to view and to pass it through the single xhtml-Files.
So i would prefer one Bean with Viewscope.

Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
  • but when I use, `redirect-faces=true`, I don't get data in next page... and if I don't use `redirect-faces=true`, in URL address I get previous page URL. – Fahim Parkar Feb 28 '13 at 13:13
  • This is fixed by extending scopes to for example- ViewAccessScope with CODI or ConversationScope with CDI. – bjedrzejewski Feb 28 '13 at 13:22
0

UserListBean can be RequestScoped or ViewScoped (ConversationalScoped in CDI), depends if you are going to do some operations on the table (ie. sorting, filtering, ...)

UserDetailBean will be RequestScoped because you need just view the details and that's it.

EditUserDetailBean is again RequestScoped because you don't need to maintain any data during requests

Or you could also make one SessionScoped bean and put everything there but I don't really recommend this, you don't want session to be too big.

So more generally speaking - Request scope for pages which don't need to maintain any data during requests, View scope when you need to store data about what's going on the page (so for example data table filtering), Session scope for storing session wide data like logged user or shopping cart and finally the Application scope for storing application wide stuff (like configuration, it serves as a Singleton bean).

Also see this article from BalusC, he described pretty well what is it all about.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115