简体   繁体   中英

Why does JSF, on request to update a portion of view instantiate all beans on the page instead of beans used in that part of page?

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM