添加记录后,数据表不会更新

时间:2013-11-27 10:19:19

标签: jsf primefaces datatable

我有一个init()函数,它返回将在Primefaces数据表中显示的电子邮件列表。我有commandButton提取电子邮件并将其保留在dataTable中。仅当我退出并登录时,这些新记录才会显示在dataTable中。  这是我的init()函数(我的托管Bean使用@ManagedBean(name = "mailMB")@SessionScoped注释):

@PostConstruct
    public void init() {
        emails = new ArrayList();      
        idCustomer = (String) session.getAttribute("idCustomer");
        System.out.println(idCustomer + " this is it");
        cust = customerBusinessLocal.findById(idCustomer);
        System.out.println(cust.getName());
        datas = dataBusinessLocal.findByCustomer(cust);
        System.out.println(datas.size());
        for (Data d : datas) {
            for (Email e : mailBusinessLocal.mailsByData(d)) {
                emails.add(e);
            }
        }

    public List<Email> getEmails() {     
        return emails;
    }

在我的xhtml页面中,我实现了以下代码:

<h:form id="form">  
        <p:commandButton value="Extract" update="tableemails" id="ajax"  widgetVar="extractButton"
                action="#{mailMB.searchEmails()}" 
                icon="ui-icon-disk" styleClass="ui-priority-primary"
                onstart="blockUIWidget1.block()"
                oncomplete="blockUIWidget1.unblock()"/>  

        <pe:blockUI target="formulaire"  widgetVar="blockUIWidget1">  
            <h:panelGrid columns="2"> 
                <img src="../../pictures/animated-loading-bar.gif" width="264" height="34" alt="animated-loading-bar"/>
                <h:outputText value="Please wait..." style="white-space: nowrap;"/>  
            </h:panelGrid>  
        </pe:blockUI>
        <p:panel id="display" header="Emails" >  
            <p:dataTable  id="tableemails" value="#{mailMB.emails}"  var="item" rowsPerPageTemplate="5,10,15" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                        paginator="true" rows="10" styleClass="case-table"  emptyMessage="No records found with given criteria" paginatorPosition="bottom" 
                        filteredValue="#{mailMB.filteredMails}" rowKey="#{item.id}" 
                        selection="#{mailMB.selectedMail}" selectionMode="single" >
                <p:ajax event="rowSelect" update=":form:display"/>            
                <p:column styleClass="blockable" filterStyle="width: 250px" filterBy="#{item.email} " sortBy="#{item.email}">
                    <f:facet name="header">
                        <h:outputText value="Email"/>
                    </f:facet>
                    <h:outputText value="#{item.email}"/>
                </p:column>
            </p:dataTable>
        </p:panel>                     
</h:form>  

1 个答案:

答案 0 :(得分:0)

看起来您在保留一些新数据后不会重新加载List<Email> emails。创建bean之后执行@PostConstract一次(每个会话创建一次@SessionScoped),这就是为什么只有在另一次登录后才能看到更改的原因。添加一个方法repopulateEmails(),它将从数据库加载数据并在添加/删除某些内容后运行它,只需将其添加到persist()方法即可。

public void persist()
{
    ....
    getEntityManager().persist(getInstance());
    getEntityManager().flush();
    emails = null;
    ......

}

public List<Email> getEmails(){
    if(emails == null){
       emails = repopulateEmails();
    }
    return emails;
}