0
votes

I have an init() function that returns a list of emails that will be displayed in a Primefaces datatable. And I have a commandButton that extract emails and persist them in the dataTable. These new records show up in the dataTable only if I log out and log in. This is my init() function ( my managed Bean is annotated with @ManagedBean(name = "mailMB") and @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;
    }

In my page xhtml i've implemented the code below:

<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

1 Answers

0
votes

it looks like you do not reload your List<Email> emails after persisting some new data. @PostConstract is executed once after your bean is created (sincce it's @SessionScoped it's created once per session), thats why you see changes only after another login. Add a method repopulateEmails() that would load data from database and run it after you add/remove something, just add it to your persist() method.

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

}

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