与Scopes的JSF问题

时间:2014-09-09 14:20:15

标签: jsf

以下是我的托管bean代码:

public class SelectEntries implements Serializable {

private static final long serialVersionUID = 1217595612573680L;

private List<SelectEntry> selectEntries = new ArrayList<SelectEntry>();

private SelectEntry selectEntry;

@PostConstruct
private void init() {
    selectEntries.add(new SelectEntry("1001", new CheckEntry("Line 1"),
            new CheckEntry("Line 2"), new CheckEntry("Line 3")));
    selectEntries.add(new SelectEntry("1002", new CheckEntry("Line 4"),
            new CheckEntry("Line 5"), new CheckEntry("Line 6")));
    selectEntries.add(new SelectEntry("1003", new CheckEntry("Line 7"),
            new CheckEntry("Line 8"), new CheckEntry("Line 9")));
}

public List<SelectEntry> getSelectEntries() {
    return selectEntries;
}

public void setSelectEntries(List<SelectEntry> selectEntries) {
    this.selectEntries = selectEntries;
}

public SelectEntry getSelectEntry() {
    return selectEntry;
}

public void setSelectEntry(SelectEntry selectEntry) {
    this.selectEntry = selectEntry;
}

public String getTemplatesString() {

    String templatesString = "";
    for (CheckEntry oneCheckEntry : getSelectEntry().getCheckEntries()) {
        if (oneCheckEntry.getCheck()) {
            templatesString += oneCheckEntry.getName();
        }
    }

    return templatesString;
}
}

这是SelectEntry类:

public class SelectEntry implements Serializable {

private static final long serialVersionUID = 7298341984562171094L;

private String name;

private List<CheckEntry> checkEntries = new ArrayList<CheckEntry>();

public SelectEntry(String name, CheckEntry... checkEntries) {
    this.name = name;
    if (checkEntries != null) {
        this.checkEntries.addAll(Arrays.asList(checkEntries));
    }
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<CheckEntry> getCheckEntries() {
    return checkEntries;
}

public void setCheckEntries(List<CheckEntry> checkEntries) {
    this.checkEntries = checkEntries;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    SelectEntry other = (SelectEntry) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

CheckEntry类:

public class CheckEntry implements Serializable {

private static final long serialVersionUID = 1730874183104100662L;

private String name;

private Boolean check;

public CheckEntry(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Boolean getCheck() {
    return check;
}

public void setCheck(Boolean check) {
    this.check = check;
}

}

我在托管bean中有一个简单的函数来创建一个基于其选择了检查条目的字符串。这只能用于SessionScope,如果我使用RequestScope,CheckEntries的布尔值为null,我得到运行时错误。为什么会这样?

1 个答案:

答案 0 :(得分:0)

根据JEE7教程:

  

请求(@RequestScoped):在单个HTTP期间,请求范围仍然存在   请求在Web应用程序中。在像hello1这样的应用程序中   该应用程序由一个请求和响应组成,即bean   使用请求范围。

因此,一旦您的页面被加载,resquest范围的支持bean就会消失,以便进行后续交互(甚至是ajax)。

您应该注意,鼓励使用包javax.enterprise.context中的范围 对于您的应用程序,看起来像ViewScope所需的最小范围。由于javax.enterprise.context未实现视图范围,因此您可以考虑使用Omnifaces