显示递增计数器值

时间:2014-03-11 13:23:44

标签: ajax jsf-2 primefaces increment jsf-2.2

我有一个电子邮件提取过程。我有一个计数器,在n封电子邮件之间提取电子邮件后递增。在电子邮件提取过程中,会显示一个带有加载徽标的对话框,并在所有电子邮件成功提取后消失。问题是如何取消在此对话框中递增的计数器值?

<p:dialog  widgetVar="blockUIWidget1" header="Hitonclick"  modal="true"    
resizable="false" closable="false"  >  
   <h:form id="blockdownload">
        <table border="0" style="width: 500px">
                <tbody > 
               <tr>  
                <td>
               <p:graphicImage url="pictures/loading81.gif" width="200" height="200"
                 alt="animated-loading-bar"/> </td>
              <td>
           <h:outputLabel value="Extracting is in progress. Please wait..."/>
             <h:outputText value="#{mailMB.c}" id="compteur" > </h:outputText>
             <div align="center">
           <p:commandButton style="width: 100px;height: 40px"   value="Cancel" 
            styleClass="ui-priority-primary" title="Cancel" />  </div>
           </td>
            </tr>
            <div align="right"></div>
           </tbody>
        </table>
    </h:form>
</p:dialog>

Managed Bean:

    public void searchEmailsR() throws Exception {
    idCustomer = (String) session.getAttribute("idCustomer");
    System.out.println(idCustomer + " this is it");
    customer = customerBusinessLocal.findById(idCustomer);
    data = dataBusinessLocal.createData(new Date(), number, keyword, moteur, customer, State.REJECTED);
    ArrayList<Email> emailsList = new ArrayList<>();
    ArrayList<String> mailsR = mailBusinessLocal.getEmailsList(keyword, number, moteur);
    System.out.println(mailsR.size());
    for (int j = 0; j < mailsR.size(); j++) {
        Email createdMail = mailmanagerLocal.createEmail(mailsR.get(j));
        emailsList.add(createdMail);
        c++;
        System.out.println(emailsList.get(j));
        System.out.println("compteur= " + c);
    }
  //  mails = mailBusinessLocal.createEmails(keyword, number, moteur, data);
    System.out.println("Method was invoked");
}

按钮调用搜索方法:

  <p:commandButton   value="Start" style="width: 12%;height: 100%"  
   update="mainform:blockdownload:compteur, :confirmPurchase, :confirmPurchaseTest, 
  :mainform" id="extractbutton" ajax="true" widgetVar="ButtonExtract" 
   actionListener="#{mailMB.searchEmailsR()}" 
   icon="ui-icon-disk" styleClass="ui-priority-primary"
   onstart="blockUIWidget1.show();" 
   oncomplete=" blockUIWidget1.hide(); 
  if (args &amp;&amp; !args.validationFailed) freeMails();return alert('Extraction is 
  finished');"> 
  </p:commandButton> 

1 个答案:

答案 0 :(得分:0)

在这里,您已经有了一个易于实现的测试用例,用于在对话框中显示计数器。我已经使用Primefaces poll每秒发出ajax请求以更新计数器值。只有在处理任务时,对话框才会保持可见:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <h:form id="form">
        <p:commandButton value="Send e-mails" action="#{bean.sendEmails}"
            onclick="dialog.show()" />
        <p:dialog widgetVar="dialog" id="dlg" modal="true" closable="false"
            visible="#{bean.showDialog}">
            <h:outputText value="Sent:#{bean.sent} out of 5" id="txt" />
            <p:poll interval="1" update="dlg" listener="#{bean.pollListener}" />
        </p:dialog>
    </h:form>
</h:body>
</html>
@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private int sent = 0;

    private boolean showDialog = false;

    /**
     * Thread which takes a random interval to complete
     */
    class FakeThread implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(new Random().nextInt(5000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sent++;
        }
    }

    public void sendEmails() throws InterruptedException {
        showDialog = true;
        System.out.println("Sending emails");
        for (int i = 0; i < 5; i++) {
            // Replace this by your real sending process
            new Thread(new FakeThread()).start();
        }
    }

    public void pollListener() {
        if (sent == 5 && showDialog) {
            System.out.println("Hidding...");
            showDialog = false;
        }
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public int getSent() {
        return sent;
    }

}

对于一个真实案例,由于邮件发送线程可能无法访问计数器字段(至少它不应该),您需要实现Observer pattern到能够通知每个已完成任务的视图。

另见: