0

I am noticing that when I use h:commandButton to call an actionlistener method from a managedbean, managed beans that are used on current view page, are re-instantiated on their own. Now since my data from those beans was already fetched and added to the view, there is no need to re-instantiate those beans, but JSF does that.

I used the following code to call a method,

<h:commandButton value="Increment" actionListener="#{channelController.increment()}"/>

I found that another managed bean UserChannelsList is instantiated on its own.. I am not submitting any data to this bean, or using any of its method, neither the view needs to fetch any properties from this bean. Why does JSF instantiates all these beans used in the view, on its own ?

EDIT

It is not due to actionListener attribute, even if I remove this attribute & just click on the commandButton then too the managed bean's are instantiated on their own!

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

1 Answers1

0

All managed beans which are referenced in the entire view or as managed property by one of the beans referenced in the view will be created if not in the current scope yet. So surely your view must have either directly or indirectly referenced it. If you can't immediately seem to find it in your code base, put a breakpoint on the bean's constructor and check the call stack to figure the initiator.

This has nothing to do with using action listeners (which is by the way fishy in the context as you have there without a real action method, read on about Differences between action and actionListener).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    this request bean has been referenced in my xhtml code but I am not updating my page or any part of it, so will this bean be still instantiated,but what is the need for this ? this seems unnecessary, isnt it ? – Rajat Gupta Dec 24 '11 at 03:43
  • i am using @postconstruct in those beans to fetch the data from DB and this makes unnecessary queries.. – Rajat Gupta Dec 24 '11 at 03:46
  • You're sending a new request on the view, surely the beans will be reinstantiated. Make them view scoped instead if you don't want to reinstantiate them on every postback on the same view. This has nothing to do with whether you're going to partially update the view or not. – BalusC Dec 24 '11 at 04:35
  • 1
    Making it view scoped means I would have to preserve the data of that bean in memory for long which doesn't make any sense for my case. I am sending a new request but that is to submit data to another bean (my whole point why the other beans referenced in the xhtml page need to be instantiated, what's the need, I am not at all using those beans anyhow & neither is the view using it) – Rajat Gupta Dec 24 '11 at 04:48
  • The view is definitely using it during restore view, otherwise they were not created. Just don't use a stateful component based MVC framework if you want to avoid this. – BalusC Dec 24 '11 at 05:28
  • ok.. but how does that make sense for JSF to do that? If I am fetching a big amount of data, & allowing some actions on that data in the same page, through command buttons, then either I need to keep this bean with all the data in the view scope or I have to re-instantiate the bean on each request that'll be sent on pressing commandButton. Either way seems expensive, and I believe this is a very common use case which may exists in most applications, how is it affordable at all in any application? & I still dont see any reason why at all JSF feels need to re-instantiate all beans in the view.. – Rajat Gupta Dec 24 '11 at 05:42
  • when i am not updating the page, how will the view use it all all ? – Rajat Gupta Dec 24 '11 at 05:45
  • 1
    That's by spec. It's sometimes stupid, yes. Just use `` to trigger a real partial submit/update and don't reference the bean inside the same `` (assuming that you've partial state saving turned on, as by default). – BalusC Dec 24 '11 at 05:46