aspose file tools*
The moose likes JSF and the fly likes Where to call service methods? backing bean?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Where to call service methods? backing bean??" Watch "Where to call service methods? backing bean??" New topic
Author

Where to call service methods? backing bean??

S Bhanu
Greenhorn

Joined: Feb 28, 2005
Posts: 15
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

Joined: Jun 25, 2001
Posts: 16058
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
S Bhanu
Greenhorn

Joined: Feb 28, 2005
Posts: 15
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

Joined: Jun 25, 2001
Posts: 16058
    
  21

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Where to call service methods? backing bean??