Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to call service methods? backing bean??

 
S Bhanu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using JSF in my application. Though the application works fine, I have some doubts about the way it's implemented.

The backing bean code looks like this:




In my xhtml page I get my attributes by using
#{colaPage.cola.name}
#{colaPage.cola.taste}
#{colaPage.cola.temperatue}
...
..

Every time I call getCola() from the page, it calls cola list, selects best cola, etc, etc.

e.g. #{colaPage.cola.name} would transform to colaPage.getCola().getName()
similarly the other calls will be like that.

Can I modify my code so that getColaList() and selectBestCola() are called only once and not at every getCola() call from the page?

Thanks for your suggestions
S Bhanu
[ April 30, 2008: Message edited by: S Bhanu ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18009
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the bean is in request scope, I'd make cola be a property and either initialize it in the constructor (if possible) or on the first getCola call. Once the cola property is set, you can bypass the selection process.

For beans in session scope of higher, it's a little trickier, since not only do you not have automatic notification that this request is a new load of the page, you might have users opening multiple windows into the session, so multiple copies of the page could be displayed. "Best" might even change between the initial page display and update page displays, so you might have to decide what the appropriate thing to do would be for your app.
 
S Bhanu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling in constructor seems a good option. Thanks

For the second solution could you please elaborate?
As I have multiple calls to the colaPage in the same screen
...
...
#{colaPage.cola.name}
#{colaPage.cola.taste}
#{colaPage.cola.temperatue}
...
How can this be resolved into display and update page displays?

Regards,
S Bhanu
[ May 02, 2008: Message edited by: S Bhanu ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18009
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that "colaPage" is a backing bean in Request scope or higher, it will be created when first referenced. Any other references from the same JSF page will refer to that bean.

If you initialize the member variable (property) cola in colaPage in the constructor for the colaPage bean, the getCola() method can return the cola object with minimal overhead. The cola object's getters would provide the actual data, once again with minimal overhead.

So all the heavy work is done once. After that's it's just a matter of getting object properties.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic