3

我有三个托管 bean:一个会话范围(S)和两个视图范围(A,B)。我想在 S 和 B 中都使用 A 的功能。但问题是在会话范围的一个中注入视图范围的 bean 是不可能的。

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

我不想复制 A 的功能。任何想法?

4

3 回答 3

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?

于 2014-01-22T08:20:44.110 回答
1

错误很明显。会话范围大于视图范围。因此,您不能在会话范围内使用它。你必须改变你的范围。

您将 bean A 声明为视图范围,这意味着您不希望它在视图更改后继续存在。所以在会话范围内注入它是在滥用它的规则。

于 2014-01-22T06:45:38.003 回答
0

我想到了。JSF 改变了你注入东西的方式。看下面的正确方法:

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

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

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

     **** your code ****

}

不要@ManagedBean在上面使用!!!!注意:UserFEnd是一个会话 bean。

希望这可以帮助。

于 2017-04-28T23:14:43.333 回答