【问题标题】:multiple command button in a form not working as expected in Primefaces DataTablePrimefaces DataTable中表单中的多个命令按钮无法按预期工作
【发布时间】:2017-08-27 15:24:04
【问题描述】:

我有一个DataTable,每一行都提供了一个commandButton来下载一个特定的文件。

<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 中&lt;f:param name="paperId" value="#{paper.paperId}"/&gt; 的值都将始终相同,就像它在第一次下载后不会更新托管 bean 中的值一样按钮被点击。这是我在 ManagedBean 中的 downloadPaper 函数

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

此问题导致用户希望下载的论文始终与他/她下载的第一个论文相同。例如,我点击第一行的下载按钮,然后我点击第二行的下载按钮,但下载的文件仍然与前一个相同,总是。我能知道为什么吗?如何解决这个问题?为了使selection 工作,我必须用表单包装整个数据表。关于如何解决这个冲突的任何想法?对不起我的英语。

【问题讨论】:

    标签: jsf primefaces datatable


    【解决方案1】:

    通过使用另一种方法将参数传递给bean解决了这个问题。
    而不是使用&lt;f:param&gt;标签传递参数

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

    不知道诀窍在哪里,但它有效。希望它可以帮助那里的一些人

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2017-06-01
      • 2019-07-31
      相关资源
      最近更新 更多