Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何在SesionScoped bean中使用ViewScoped bean?_Jsf_Jsf 2_Dependency Injection_View Scope_Session Scope - Fatal编程技术网

Jsf 如何在SesionScoped bean中使用ViewScoped bean?

Jsf 如何在SesionScoped bean中使用ViewScoped bean?,jsf,jsf-2,dependency-injection,view-scope,session-scope,Jsf,Jsf 2,Dependency Injection,View Scope,Session Scope,我有三个托管bean:一个会话范围(S)和两个视图范围(A,B)。 我想在s和B中使用A的功能。 但问题是在会话范围的bean中注入视图范围的bean是不可能的 The scope of the object referenced by expression #{a}, view, is shorter than the referring managed beans (s) scope of session 我不想复制A的功能。 有什么想法吗?错误很明显。会话范围大于视图范围。因此,您不能在

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

The scope of the object referenced by expression #{a}, view, is shorter than the referring managed beans (s) scope of session
我不想复制A的功能。
有什么想法吗?

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


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

这只是表明模型中存在设计问题。这表明视图范围的bean类A有“太多”的代码逻辑,它的代码应该被重构成一个不同的、可重用的类,而会话范围的bean类S和视图范围的bean类A都可以使用这个类。也许它代表了实际需要在EJB中的业务服务代码

在任何情况下,您都可以通过将视图范围的bean作为会话范围的bean的操作方法的方法参数传递来实现需求

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


但这也是一种设计的味道。您需要绝对确保为bean持有的数据/状态选择正确的范围。另请参见

我找到了答案。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


希望这有帮助。

您可以将A的范围更改为sessionscope或将s的范围更改为ViewScope我真的需要A处于视图中,s处于会话中!不,你不能用它。即使你这样做了,也没有任何意义。如果希望在会话范围bean而不是viewscope中访问变量,那么应该将它们放在会话范围bean中。共享您的代码,以便我可以为一些解决方案提供更多帮助。这些解决方案应该只关注会话问题。永远不要谈论A的细节,因为它必须与视图相关。您可以改为访问S,以通知其更改。这与请求的完全相反