【发布时间】: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 中<f:param name="paperId" value="#{paper.paperId}"/> 的值都将始终相同,就像它在第一次下载后不会更新托管 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