• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

JSF Basic model question :-(

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Dushy Inguva
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic