3

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?

Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
Farvardin
  • 5,336
  • 5
  • 33
  • 54

3 Answers3

6

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?

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

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.

Madhura
  • 551
  • 5
  • 18
  • You can change A's scope to sessionscope or S's scope to viewscope – Madhura Jan 22 '14 at 06:52
  • I really need to A be in view and S in session! – Farvardin Jan 22 '14 at 06:57
  • 2
    No you cannot use it. And even if you did, it wont make any sense. If you want your variables to get accessed in session scoped bean rather than viewscope, you should place them in your session scope bean. Share your code so that i can help more for some workaround – Madhura Jan 22 '14 at 08:42
  • 1
    S should be worried only about session matters. Never about A's specifics, which must be view-related. You can instead make A access S, to notify it about changes. – Aritz Jan 22 '14 at 12:15
0

I figured it out. JSF changed the way you inject things. See below the right way:

@Named(value = "propertyFEnd")
@ViewScoped
public class PropertyFEnd implements Serializable {

    @Inject @ManagedProperty("#{userFEnd}")
    private UserFEnd userfend;

     **** plus getter/setter for userfend ***

     **** your code ****

}

Do not use @ManagedBean on top!!!! Note: UserFEnd is a session bean.

Hope this helps.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
JediSal
  • 65
  • 4