gpt4 book ai didi

java - 如何获取并使用 session 对象来填充 JSF 中的数据表?

转载 作者:行者123 更新时间:2023-12-02 06:54:49 25 4
gpt4 key购买 nike

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

@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>

但问题是,由于我将客户保留在 session 中,因此 value="#{customer.accounts}"返回 null,因为它实际上是 null。我以某种方式需要获取当前 session 对象。我尝试过:

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

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

谢谢

最佳答案

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

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

此外,在继续学习之前,最好先关注 JSF 基础知识。

关于java - 如何获取并使用 session 对象来填充 JSF 中的数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com