Primefaces – dynamic确认对话框内容

我有一个上传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> 

Managed 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()方法。

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

你需要将你的方法分解成(至less)2步骤。

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

  • 让用户select文件
  • 在“上传” – 点击,上传,分析
  • 询问用户是否要处理文件。
    • 如果是:处理它。
    • 如果没有:放下它。

@ViewScoped@ConversationScoped@FlowScoped注释可能会帮助您完成此任务,具体取决于您的需要。

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

最后,我的解决scheme基于JSF和Managed 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(); } } 

托pipeBean代码:

 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); } 

不是一个优雅的解决scheme,但解决了我的问题。