表单中的多个命令按钮在Primefaces中无法正常工作DataTable

问题描述:

我有一个DataTable,每行都提供了一个commandButton来下载特定文件。表单中的多个命令按钮在Primefaces中无法正常工作DataTable

<p:dataTable value="#{studentClassroomBean.papers}" var="paper" id="paperDT" 
      paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
      paginator="true" rows="10" rowsPerPageTemplate="5,10,15" paginatorPosition="bottom" 
      rowKey="#{paper.paperId}" style="margin: 20px 20px 20px 20px;" 
      selection="#{studentClassroomBean.selectedPapers}"> 
    <p:column selectionMode="multiple" style="width: 16px; text-align:center;"/> 

    <p:column headerText="Download" style="text-align: center; width: 80px"> 
     <h:form> 
      <p:commandButton icon="fa fa-download" actionListener="#{studentClassroomBean.downloadPaper}" 
          ajax="false"> 
       <f:param name="paperId" value="#{paper.paperId}"/> 
       <p:fileDownload value="#{studentClassroomBean.downloadFile}"/> 
      </p:commandButton> 
     </h:form> 
    </p:column> 

    <f:facet name="footer"> 
     <p:commandButton value="Download Papers" icon="fa fa-download" ajax="false" 
         actionListener="#{studentClassroomBean.batchDownload}" process="paperDT"> 
      <p:fileDownload value="#{studentClassroomBean.downloadZip}"/> 
     </p:commandButton> 
    </f:facet> 
</p:dataTable> 

这工作完全如果我只用形式包装commandButton而不是包住整个dataTable。如果使用表单包装整个dataTable,则在managedBean中的值为<f:param name="paperId" value="#{paper.paperId}"/>将始终保持不变,无论您单击哪个按钮,都不会在单击第一次下载按钮后更新托管bean中的值。这是我在ManagedBean

public void downloadPaper() throws IOException 
{ 
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); 
    Paper selectedPaper = paperService.findByPaperId(params.get("paperId")); 
    String fullPath = DIRECTORY + selectedPaper.getFileName(); 
    String contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(fullPath); 
    String fileName = selectedPaper.getTitle() + "." + FilenameUtils.getExtension(fullPath); 
    InputStream input = new FileInputStream(new File(fullPath)); 
    downloadFile = new DefaultStreamedContent(input, contentType, fileName); 
} 

这个问题导致该用户想下载的永远是同一篇论文为他/她下载的第一纸张的纸张downloadPaper功能。例如,我点击第一行的下载按钮,然后点击第二行的下载按钮,但下载的文件仍然与上一个一样。我能知道为什么吗?如何解决这个问题?为了使selection有效,我必须将整个dataTable封装成表格。有关如何解决这一冲突的任何想法?对不起我的英语不好。

该问题通过使用另一种方法将参数传递给bean来解决。
,而不是使用<f:param>标签

<p:column headerText="Download" style="text-align: center; width: 80px"> 
    <p:commandButton icon="fa fa-download" actionListener="#{studentClassroomBean.downloadPaper}" 
      ajax="false"> 
     <f:param name="paperId" value="#{paper.paperId}"/> 
     <p:fileDownload value="#{studentClassroomBean.downloadFile}"/> 
    </p:commandButton> 
</p:column> 

传递参数我改成了这样:

<p:column headerText="Download" style="text-align: center; width: 80px"> 
    <p:commandButton icon="fa fa-download" actionListener="#{studentClassroomBean.downloadPaper(paper.paperId)}" 
        ajax="false"> 
     <p:fileDownload value="#{studentClassroomBean.downloadFile}"/> 
    </p:commandButton> 
</p:column> 

,赶上参数在这样的托管bean:

public void downloadPaper(String paperId) throws IOException 
{ 
    Paper selectedPaper = paperService.findByPaperId(paperId); 
    String fullPath = DIRECTORY + selectedPaper.getFileName(); 
    String contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(fullPath); 
    String fileName = selectedPaper.getTitle() + "." + FilenameUtils.getExtension(fullPath); 
    InputStream input = new FileInputStream(new File(fullPath)); 
    downloadFile = new DefaultStreamedContent(input, contentType, fileName); 
} 

不知道技巧在哪里,但它的作品。希望它有助于一些人在那里