【问题标题】:Display incrementing counter value显示递增计数器值
【发布时间】:2023-03-16 17:03:01
【问题描述】:

我有一个电子邮件提取过程。我有一个计数器,在提取 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>

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

【问题讨论】:

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


    【解决方案1】:

    这里有一个易于实现的测试用例,用于在对话框中显示计数器。我使用 Primefaces 轮询每秒发出 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 以便能够通知每个视图完成任务。

    另请参阅:

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 2023-02-19
      • 2017-03-03
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多