如何获取和使用会话对象来填充JSF中的数据表? [英] How to get and use a session object to populate a datatable in JSF?

查看:251
本文介绍了如何获取和使用会话对象来填充JSF中的数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的jsf页面中填充一个datatable。这是我做的:我先添加用户到登录系统登录到我的LoginBean的会话:

  @ManagedBean name =loginBean)
@Dependent
@SessionScoped
public void loginCheck(){
Customer currentCustomer = new Customer(rs.getString(1),rs.getString ),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),accounts);
FacesContext.getCurrentInstance()。getExternalContext()。getSessionMap()。put(currentCustomer,currentCustomer);
//我从
//中跳过用户名和密码检查,并从数据库获取客户信息
//不要使代码太日志,我在我的项目中执行
}

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

  @ManagedBean(name =customer)
@SessionScoped
public class Customer implements Serializable {
private List< Account>账户

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

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

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

 < h:form> 
< h:dataTable id =accountsTablevalue =#{customer.accounts}var =account>
< h:column>
< f:facet name =header>帐号< / f:facet>
#{account.accountNumber}
< / h:column>
< / h:dataTable>
< / h:form>

但问题是,由于我让客户在会话中,value =#{customer account}返回null,因为它真的为空。我以某种方式需要获取当前会话对象。我尝试:

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

但是在jsf页面中看不到FacesContext()我正在考虑先获取客户对象,然后到达其列表,它是一个ArrayList。那怎么办呢?可以帮助吗?



谢谢

解决方案

其关键是:在EL语境中#{currentCustomer}



为了您的信息,会话属性映射可以通过隐式EL对象使用:#{facesContext} #{sessionScope} 后者当然也可以用来在会话映射中获取以前放置的对象。



此外,在继续使用JSF基础之前,请​​务必专注于JSF基础知识。


I am trying to populate a datatable in my jsf page. Here is what i do: I first add the user to the session who logins to the system in my 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
}

Then i want to fill a datatable with current user's accounts information. Here is my Customer class's related parts:

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

And here is the jsf page i populate my data:

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

But the problem is, since i keep the customer in the session, value="#{customer.accounts}" returns null because it is really null. I somehow need to get the current session object. I tried:

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

But it does not see the FacesContext() in a jsf page. I was thinking to get the customer object first, and then reach its accounts list, which is an ArrayList. So how can do this? Can anyone help?

Thanks

解决方案

You can get it via its key: #{currentCustomer}, in EL context.

And for your information, both current faces context and session attribute map are available via implicit EL objects: #{facesContext} and #{sessionScope} respectively. The latter can of course be used to get the previously put object in session map as well.

Also it'd be wise to concentrate on JSF basics before moving on any further.

这篇关于如何获取和使用会话对象来填充JSF中的数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆