wood burning stoves 2.0*
The moose likes Struts and the fly likes JSF Basic model question :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSF Basic model question :-(" Watch "JSF Basic model question :-(" New topic
Author

JSF Basic model question :-(

Dushy Inguva
Ranch Hand

Joined: Jun 24, 2003
Posts: 264
I have a page called FirstPage.jsp
This has a request scope backing bean called FirstPage.java

This bean has 2 get methods. In both, I check if the data is loaded, and if not, load the data and cache it for the other get method

public class FirstPage{

private MyModel model;

public MyModel getModel() {
if(model == null) {
loadModel();
}
return model;
}

private void loadModel(){...}

public void saveModel() {
Database.instance().save(model);
}
}

I have a navigation rule which has to show SecondPage after the saveModel button is clicked and saveModel() method is performed.

The following timeline happens
1. I point browser to URL
2. an instance of FirstPage is created
3. getModel method is called (and model gets loaded)
4. Page gets displayed on browser
5. I make changes to the page
6. I hit saveModel button
7. An instance of FirstPage gets created AGAIN !!!
8. getModel method on FirstPage is called AGAIN and model gets loaded AGAIN
9. Values are updated on the model object
10. saveMethod is called
11. SecondPage is displayed

My questions are:
1. WHY does it invoke the getModel again ?
2. Isn't it kind of convoluted that for every database update operation, 2 reads (getModel is invoked at two different times) and a write takeplace in JSF?
3. Am i confused or is this really this way?


SJCP, SCBCD, SJCD, SCDJWS, SCEA (Part I)
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Your backing bean is in the Request scope. Put it in the session scope to avoid mulitple calls to db.

Nowhere in the JSF specification it has been mentioned that getter/setters would be called only once. From Apply Request Value to Render Response phase, the page passes through different life cycles and hence its normal.
[ November 30, 2004: Message edited by: K Varun ]

- Varun
Dushy Inguva
Ranch Hand

Joined: Jun 24, 2003
Posts: 264
Note that session scoped jsf beans are inturn placed in the HttpSession object by the jsf provider.
Following popular design principles, the amount of data placed in the session has to be minimized. And, something is session worthy, ONLY if it is being used across the application. NOT as a way to pass parameters between pages, or else, the session WILL get bloated quite quickly.

And yes, the JSF spec unfortunately never mentions that my get methods will be called only once. But, what we endup is double database access even for readonly pages. Thats not quite bright!!!
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
One way to get avoid to have seesion scoped beans, is to put the model/data in the jsf componet itself. ex the model in i jsf table
You can have the dao object loaded outside the jsf to avoid serious load issios and have them in appscope so the req beans can access them.

//Rille
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF Basic model question :-(
 
Similar Threads
Struts 2 parameter mapping issue
Using a jsf DAO interface
How to store the data entered by user in a form into a database using struts
How do you manage connections?
A multi-step wizard form with multiple <ui:include>s