【问题标题】:Primefaces - Dynamic confirm dialog contentPrimefaces - 动态确认对话框内容
【发布时间】:2015-08-10 20:08:02
【问题描述】:

我有一个上传 Excel 电子表格的 JSF 页面。该电子表格有一个包含一定数量行的工作簿。在处理每一行之前,我想把它全部数一遍,然后问用户“确认处理 X 行?”

这是我的 JSF 代码(cancel.xhtml):

<h:form enctype="multipart/form-data" id="frmMassive">
   <p:growl id="messagesUpload" showDetail="true" life="3000" />
      <h:panelGrid id="panel" columns="2">

         <h:outputText value="File" />
         <p:fileUpload value="#{cancelView.file}" mode="simple" skinSimple="true" />

         <p:spacer></p:spacer>

         <p:commandButton value="Send file" ajax="false"
            action="#{cancelView.preProcessMassive}" />
      </h:panelGrid>
   </p:panel>
</h:form>

<h:form id="frmConfirm">
   <p:commandButton style="display: none" id="confirmButton"
      actionListener="#{cancelView.postProcessMassive}">
      <p:confirm header="Confirmation"
         message="Confirm the processing of #{cancelView.lineCount} line(s)?" 
         icon="ui-icon-alert" />
   </p:commandButton>

   <p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
      <p:commandButton value="Yes" type="button" 
         styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
      <p:commandButton value="No" type="button" 
         styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
   </p:confirmDialog>
</h:form>

托管 Bean 方法 (CancelView.java):

public void preProcessMassive() {

   HSSFWorkbook wb;
   try {
      wb = new HSSFWorkbook(this.file.getInputstream());
   } catch (IOException e) {
      e.printStackTrace();
      return;
   }

   HttpSession session = Util.getSession();
   session.setAttribute("cancelLineCount", sheet.getPhysicalNumberOfRows());
   session.setAttribute("cancelSpreadSheet", file);

   RequestContext context = RequestContext.getCurrentInstance();
   context.execute("document.getElementById('frmConfirm:confirmButton').click()");
}

public int getLineCount() {
   HttpSession session = Util.getSession();
   if (session.getAttribute("cancelLineCount") == null) {
      return 0;
   } else {
      return (Integer)session.getAttribute("cancelLineCount");
   }
}

问题是我的JSF页面在上传电子表格之前调用了getLineCount()方法。

是否有另一种方法来计算电子表格的行数并在处理之前将其返回给用户?

【问题讨论】:

  • Util.getSession()?呃,为什么不只是一个会话范围的托管 bean 并注入它?

标签: excel jsf primefaces


【解决方案1】:

您需要将您的方法拆分为(至少)两步法。

在您能够确定有关工作表的任何内容之前,您(实际上是用户)必须上传它。然而,上传并不意味着处理它,您可以将上传的文件保存在 bean 属性*中,直到用户决定最终处理它。

  • 让用户选择文件
  • 在“上传”-点击、上传、分析
  • 询问用户是否要处理该文件。
    • 如果是:处理它。
    • 如果不是:放弃它。

@ViewScoped@ConversationScoped@FlowScoped 注释可能会根据您的需要帮助您完成此任务。

*对于处理 Excel 文件,某些库只能用于持久文件,不能用于内存中的对象。您也可以将其存储在临时位置。

【讨论】:

    【解决方案2】:

    最后,我的解决方案是基于 JSF 和托管 Bean 之间的 javascript 消息传递。

    JSF 代码:

    <h:form enctype="multipart/form-data" id="frmMassive">
       <p:growl id="messagesUpload" showDetail="true" life="3000" />
       <p:panel header="Massive Cancel" style="width: 640px;">
          <h:panelGrid id="pnlMassiveCancel" columns="2">
    
             <h:outputText value="File" />
             <p:fileUpload value="#{cancelView.file}" mode="simple" skinSimple="true" />
    
             <p:spacer></p:spacer>
    
             <p:commandButton value="Send" ajax="false"
                action="#{cancelView.preProcessMassive}" />
          </h:panelGrid>
       </p:panel>
    </h:form>
    
    <h:form id="frmConfirm">
       <p:remoteCommand name="postProcessMassiveCancel" 
          update="messagesConfirm" 
          actionListener="#{cancelView.postProcessMassive}" />
    
       <p:growl id="messagesConfirm" showDetail="true" life="3000" />
    </h:form>
    
    <h:outputScript library="js" name="cancelUtil.js" />
    

    cancelUtil.js:

    function confirmMassiveCancel(msg, postProcessFunction) {
        var result = window.confirm(msg);
        if (result == true) {
            postProcessFunction();
        }
    }
    

    托管 Bean 代码:

    public void preProcessMassive() {
       HSSFWorkbook wb;
       try {
          wb = new HSSFWorkbook(this.file.getInputstream());
       } catch (IOException e) {
          e.printStackTrace();
          return;
       }
    
       HSSFSheet sheet = wb.getSheetAt(0);
    
       lineCount = sheet.getPhysicalNumberOfRows();
       HttpSession session = Util.getSession();
    
       RequestContext context = RequestContext.getCurrentInstance();
       String script =  "confirmMassiveCancel('Do you want to process " 
             + String.valueOf(lineCount) + " line(s)?', postProcessMassiveCancel)";
       context.execute(script);
    }
    

    无论如何都不是一个优雅的解决方案,但解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2014-04-08
      • 1970-01-01
      • 2017-09-07
      • 2019-02-11
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多