0

我正在尝试在我的 jsf 页面中填充数据表。这是我所做的:我首先将用户添加到登录到我的 LoginBean 中的系统的会话中:

@ManagedBean(name = "loginBean")
@Dependent
@SessionScoped
public void loginCheck() {
      Customer currentCustomer = new Customer(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7),accounts);
      FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("currentCustomer", currentCustomer);
 // I skipped username and password check from
 // and getting customer information from database 
 // not to make the code too log, i do them in my project
}

然后我想用当前用户的帐户信息填充数据表。这是我的客户类的相关部分:

@ManagedBean(name = "customer")
@SessionScoped
public class Customer implements Serializable {
    private List<Account> accounts;

public void setAccounts(ArrayList<Account> accounts){
    this.accounts=accounts;
}

public List<Account> getAccounts(){
    return accounts;                                                                                                        
}

这是我填充数据的 jsf 页面:

    <h:form>
        <h:dataTable id="accountsTable" value="#{customer.accounts}" var="account">
            <h:column>
                <f:facet name="header">Account Number</f:facet>
                    #{account.accountNumber}
            </h:column>
        </h:dataTable>
    </h:form>

但问题是,由于我将客户保留在会话中,因此 value="#{customer.accounts}" 返回 null 因为它确实为 null。我不知何故需要获取当前的会话对象。我试过了:

value="#{FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentCustomer");}" 

但它在 jsf 页面中看不到 FacesContext()。我想先获取客户对象,然后到达其帐户列表,这是一个 ArrayList。那么如何做到这一点呢?任何人都可以帮忙吗?

谢谢

4

1 回答 1

2

您可以通过它的 key: 获取它#{currentCustomer},在 EL 上下文中。

供您参考,当前面孔上下文和会话属性映射都可以通过隐式 EL 对象获得:#{facesContext}#{sessionScope}分别。后者当然也可以用于获取先前放置在会话映射中的对象。

同样明智的做法是在继续学习之前专注于 JSF 基础知识。

于 2013-07-06T14:43:23.983 回答