JSF2无法从ViewScoped作为ManagedProperty访问SessionScoped bean

时间:2011-11-19 14:43:45

标签: jsf jsf-2

我有一个奇怪的问题。 Afaik我可以将一个SessionScoped bean注入一个viewscoped,因为它比另一个更广泛。这是我的代码:

@ManagedBean
@ViewScoped
public class ProjectBean implements Serializable {

@ManagedProperty(value="#{projectCurrentBean}")
private ProjectCurrentBean currentBean;

public void setCurrentBean(ProjectCurrentBean currentBean) {
    this.currentBean = currentBean;
}     

@ManagedProperty(value="#{userCredentialsBean}")
private UserCredentialsBean activeUser;

public void setActiveUser(UserCredentialsBean activeUser) {
    this.activeUser = activeUser;
}

2个托管bean:

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable  {

@ManagedBean
@SessionScoped
public class UserCredentialsBean  implements Serializable {

它适用于UserCredentialsBean,但当我把ProjectCurrentBean失败时:

Unable to create managed bean projectBean. The following problems were found: - The scope of the object referenced by expression #{projectCurrentBean}, request, is shorter   than the referring managed beans (projectBean) scope of view

为什么呢? :)

1 个答案:

答案 0 :(得分:11)

您没有使用javax.faces.bean package中的@SessionScoped来宣传该bean,而是来自javax.enterprise.context package。这与@ManagedBean包中的javax.faces.bean无法结合使用。然后,bean将默认为请求范围,其行为类似于@RequestScoped

修复您的导入。

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable {