0

I am writing simple web application for managing data in a few tables. In JSF I can make SessionScoped bean to store data across several user requests. But using many SessionScoped beans isn't good idea, I think. I want to implement PHP-like behavior (user inputs data, click "save" and data posts to the server-side function) for all tables. What is the better way to do it?

Should I use one big SessionScoped bean for all tables? (I think, no)

Should I use separate beans for each table? (I think, it's the good choice). But what's the bean scope should I use for proper design?

Is the better way to implement such application - is use RequestScoped beans with ajax? I just can't understand this moment in the JSF. I read books, but answer still unanswered.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gooamoko
  • 658
  • 12
  • 32
  • 1
    Use `@ViewScoped`. More info: [Communication in JSF 2: Managed bean scopes](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ManagedBeanScopes) – Luiggi Mendoza Sep 02 '13 at 06:33
  • @hexafraction that applies for other technologies, not for JSF 2 where this question has been asked and solved many times. The problem is that many people uses the java tag thus getting the attention of non-JSF programmers that ends in comments like yours. – Luiggi Mendoza Sep 02 '13 at 06:33
  • How other peoples make their user interfaces in JSF? What is JSF concept of user input, validation data, store data and user output in real (not books examples) application? – gooamoko Sep 02 '13 at 06:35
  • 2
    BalusC (JSF expert and experienced user on SO on Java, Java EE and JSF) shows real world cases in [his blog](http://balusc.blogspot.com/). Anyway, it will be way better if you break your [analysis paralysis](http://en.wikipedia.org/wiki/Analysis_paralysis) status and start doing some work to get specific problems and their solutions. IMO in the beginning lot of your questions would be already answered (lot of them probably in this site). – Luiggi Mendoza Sep 02 '13 at 06:38
  • 2
    Anyway, to answer your questions: *Should I use one big SessionScoped bean for all tables?* **Never**. *Should I use separate beans for each table?* It will depend on your design, but I would recommend using a managed bean per view. *what's the bean scope should I use for proper design?* Read the link in my first comment and decide. *Is the better way to implement such application - is use RequestScoped beans with ajax?* Use `@RequestScoped` or `@ViewScoped`, depending on your needs. – Luiggi Mendoza Sep 02 '13 at 06:42
  • @LuiggiMendoza In the `@ViewScoped` You need to return null or void from action (listener) methods to keep the bean alive. So what if I want to show list of items on one page and form for adding/editing item on another? Seems like it will be a different views. What can I do in this situation? – gooamoko Sep 02 '13 at 06:43
  • 2
    Again, **do something first** instead of just keep asking questions (note that links in my comments aren't for decoration purposes). I won't directly answer your question, but continue with your analysis: if you display the data to user in view A and use view B to insert or edit the data, would you need to store the list anywhere, probably in session? If so, why, will it give you any value? If you have to go back from view B to view A but the data to display has changed, then the data should be reloaded (from database or your data source), right? – Luiggi Mendoza Sep 02 '13 at 06:46
  • @LuiggiMendoza So, I use view A to show list of items. I don't need to store this list anywhere. View B - only for add/edit one concrete item's data. In view A I press button "Add" and go to view B. After filling data I press "save" and after processing request I go either back to the view B (validation error) or to the view A, that again request new data from server. In the article, I read about `@CustomScoped` bean. I think, I can use it for trans-view data storing. Right? Is there another good solutions? Thanks for wasting your time for me. – gooamoko Sep 02 '13 at 06:58
  • Assuming this is the current design, you can support view A with `@RequestScoped` bean and view B with `@RequestScoped` or `@ViewScoped` bean, depending on your needs. Since there's no data to maintain between the views, then you won't need a `@CustomScoped` bean, but again, you won't realize this just by plain reading but **working on it**. – Luiggi Mendoza Sep 02 '13 at 07:00

1 Answers1

0

Session-scoped managed beans comes at the cost of storing all those beans in memory throught the entire session.

If you want to use the bean almost throughout the entire session, then only declare them as session beans. If you think you will be done with the beans in single request then go for request scoped beans.

If you have rich ajax-enabled pages then go for view-scoped managed beans.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Chaitanya Gudala
  • 305
  • 1
  • 3
  • 22
  • Your first statement deserves a fat -1. This is utter nonsense. Just use the right scope for the data it holds. – BalusC Sep 02 '13 at 12:04
  • @BalusC : As per your comment,modified my answer – Chaitanya Gudala Sep 02 '13 at 12:15
  • So, answering in my own question, the best solution is use `@ViewScoped` managed bean and **one view for form and data list**. The main problem was in the separate views. In the BalusC blog I found interesting example of `@ViewScoped`. – gooamoko Sep 13 '13 at 08:47