Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use ViewScoped bean in SesionScoped bean?

I have Three managed bean: One session scoped (S) and Two view scoped (A,B). I want to use A's functionality in both S and B. but the problem is that injecting view scoped bean in session scoped one is impossible.

The scope of the object referenced by expression #{a}, view, is shorter than the referring managed beans (s) scope of session

I don't want to duplicate A's functionality. any idea?

like image 460
Taher Khorshidi Avatar asked Dec 27 '25 15:12

Taher Khorshidi


2 Answers

This just indicates a design problem in your model. This suggests that view scoped bean class A is having "too much" code logic and that its code should be refactored into a different, reusable class which in turn can be used by both session scoped bean class S and view scoped bean class A. Law of Demeter and such. Perhaps it represents business service code which actually needs to be in an EJB?

In any case, you could achieve the requirement by passing view scoped bean A as method argument of an action method of session scoped bean S.

<h:commandXxx ... action="#{sessionScopedBean.doSomething(viewScopedBean)}" />

But this is also a design smell. You need to make absolutely sure that you choose the right scope for the data/state the bean holds. See also How to choose the right bean scope?

like image 131
BalusC Avatar answered Dec 30 '25 14:12

BalusC


The error is pretty clear. The Session scope is larger than view scope. Hence You cannot use it in session scope. You have to change your scopes.

You are declaring your bean A as view scope, means you dont want it to live after the view is changes. So injecting it in session scope is abusing its rule.

like image 34
Madhura Avatar answered Dec 30 '25 15:12

Madhura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!