Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top