5

I have been noticing that all of my beans used in the page are unnecessarily getting instantiated on updating any small portion of the page. Ok, they are request scoped so will be instantiated on every request but it should be done only when those beans are needed by that part of the page which is being updated. Isn't it ?

Why this bad design in JSF?

Update:

I found the real culprit that was causing this behavior, I had f:event type="preRenderView" at certain places in my webpage. That actually caused those beans to be re-instantiated in order to invoke the listener. The fix was to use f:event type="preRenderComponent" instead. This reduced most of the unnecessary bean instantiations at each request but still I see a few unnecessary bean instantiations.

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
  • This is not bad design, the bad design is yours who are not understanding JSF basic concepts. If you don't want this behavior, you can change the managed bean scope to View. – Luiggi Mendoza Jul 21 '12 at 17:29
  • 2
    @LuiggiMendoza: Changing a request scoped bean to `viewScoped` just to prevent this is not the right solution as one might not want to persist the data in the bean for so long. Btw why do you think this behaviour of JSF is right ? How is useful at all in the application context ?? You mean to say that all the request scoped beans should be converted to viewscoped when you need to partially update your pages, or otherwise let the JSF unnecessarily fill all the beans with data that is no longer required, right ? Then for what good purpose these request scoped beans are useful ? – Rajat Gupta Jul 21 '12 at 18:36
  • 2
    @downvoters: please let me know how can I improve my question or reason of downvote. Thanks! – Rajat Gupta Jul 21 '12 at 18:37
  • 1
    @user01 +1 I also think this question is valid! It would be nice to add some example code to explicit what you mean, though... – Elias Dorneles Jul 21 '12 at 18:50
  • That's the purpose for ViewScope, read [Does view scope bean survive Navigation JSF](http://stackoverflow.com/a/4831963/1065197). It will not live so long as you think. And yes, I guess it is right to have the Request been created every time you make a request, this will leverage lots of concurrency problems, it is useful if you know how to use it properly, there are also Session and Application scope, for example if I have a pattern or a list that I would use every time on the application, I would use ApplicationScope instead of Request. – Luiggi Mendoza Jul 21 '12 at 18:51
  • A request can become useful when you're doing a login form, because the user and password info must travel once per request. When the user has been authenticated, you can use the data to save it in a SessionScope bean (that will live on the user Session). And if you're not agree with either of these scopes (or don't find a good purpose for them) you can set the scope using [CustomScope](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html). – Luiggi Mendoza Jul 21 '12 at 18:54
  • And after read more info and get involved in real projects with JSF, you will see that **it is not a bad design**. – Luiggi Mendoza Jul 21 '12 at 18:54
  • @LuiggiMendoza: I could filter out only one relevant statement from your above comments..."_I guess it is right to have the Request been created every time you make a request, this will leverage lots of concurrency problems_," -- how ? Anyways I agree that request scoped, by design, would be instantiated on each request but why also those request scoped bean that are not required in the portion to be updated on the page must be instantiated ? – Rajat Gupta Jul 21 '12 at 19:01
  • 2
    @LuiggiMendoza Please, note that the OP is asking about partial updates specifically. The OP would please correct me if I'm wrong, but maybe the question could be rephrased to: "Why does JSF instantiate beans which state is never actually needed in an AJAX request?" – Elias Dorneles Jul 21 '12 at 19:29
  • @eljunior: Yes you're right! but I think the current title expresses my concern in even more simpler terms..:) Btw feel free to edit. – Rajat Gupta Jul 21 '12 at 20:39
  • 1
    This happen using execute attribute in f:ajax? – Guilherme Torres Castro Jul 23 '12 at 20:49

2 Answers2

4

In such case, the bean will only be constructed when it's involved in building the view (read: one of its properties is been referenced in any attribute of a taghandler or the id or binding attribute of an UI component). Otherwise it won't be constructed. At least, I can't reproduce your problem in Mojarra 2.1.11 (and also not in 2.0.0) when designing a JSF view "the usual way".

A view cannot be partially built, but its state can be partially saved and restored and the UI component tree can be partially rendered.

Your "bad design in JSF" complaint holds no argument.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

Hopefully I've interpreted your question correctly!

Your page is being submitted by a form, so the Component Tree is retrieved or constructed for the first time (first step of the JSF lifecycle).

The Component Tree is made up of everything, not just the View you are currently on.

Any JSF component which is bound to any of your request scope beans are therefore instantiated during this phase.

A good example of this would be a tabView. Imagine a tabView with four tabs, each bound to a different page, which are each bound to a corresponding request-scoped backing bean.

Despite not all of the tabs being visible at the same time, they are all part of the Component Tree and so, if you set a breakpoint, you'll see the beans being instantiated for each page for the request phase.

8bitjunkie
  • 12,793
  • 9
  • 57
  • 70