3

我有这个奇怪的问题,文件下载选项没有从 XHTML 中调用。我对 JSF 很陌生,所以我可能搞砸了一些基本的东西,但任何帮助都将不胜感激。

这是我的 XHTML

<p>Files List</p>
<h:form prependId="false">
    <p:dataTable value="#{pc_ArchiveFiles.archiveFiles}" var="fs"
        paginator="true" rows="5"
        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
        rowsPerPageTemplate="5,10,15">
        <f:facet name="header">  
            Client Files  
        </f:facet>
        <p:column>
            <f:facet name="header">
                File Name
            </f:facet>
             <h:outputText value="#{fs.fileName}" /> 
        </p:column>
        <p:column>
            <f:facet name="header">
                File Size
            </f:facet>
            <h:outputText value="#{fs.fileSizeInKB}" />
        </p:column>
        <p:column>
            <f:facet name="header">
               Download File
            </f:facet>
            <p:commandLink id="downloadLink" value="Download" ajax="false">  
                    <p:fileDownload value="#{pc_ArchiveFiles.downloadPDF}" />
            </p:commandLink> 
        </p:column>
    </p:dataTable>
</h:form>

这是后备豆

@ManagedBean(value = "pc_ArchiveFiles")
@RequestScoped
public class ArchiveFiles extends PageCodeBase {
private static final Logger logger = LoggerFactory
        .getLogger(ArchiveFiles.class);
 @Value("${archive.location}")
private String repository;
public List<ArchiveFile> getArchiveFiles() {


    Map<String, String> params = FacesContext.getCurrentInstance()
            .getExternalContext().getRequestParameterMap();
    subCategory = params.get("categoryName");

    // Build request object
    ArchiveFilesRequest request = new ArchiveFilesRequest();
    request.setCaseWorkerId(SecurityUtil.getLoggedInUser().getLoggedUserId());
    request.setClientId(getSelectedConsumer().getConsumerId());
    request.setCategoryName(subCategory);
    ArchiveFilesResponse response = archiveService.getArchiveFiles(request);
    if((response.getResponseType() == ResponseType.SUCCESS || response.getResponseType() == ResponseType.WARNING) && response.getFileCount() > 0) { // There is at least one file
        archiveFiles = new ArrayList<ArchiveFile>();
        archiveFiles.addAll(response.getFileSet());
        return archiveFiles;
    } else {
        return Collections.<ArchiveFile> emptyList();
    }
}


 public void downloadPDF() throws IOException {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        facesContext.responseComplete();
    }

}

显示下载链接的 JSF 页面

当我单击下载时,我希望调用 downloadPDF()。但它似乎再次调用 getArchiveFiles() 方法而不是 downloadPDF()。我已将 p:CommandLink 更改为以下代码,但它仍然没有调用正确的方法。最重要的是,如果我让它工作,我还必须传入文件名参数。

<h:commandLink id="downloadLink" value="Download PDF" target="_blank" action="#{pc_ArchiveFiles.downloadPDF}" />
4

1 回答 1

0

p:fileDownloadvalue 属性应该是StreamedContent 的一个实例在第 174 页的此处查看文档,它应该类似于以下代码:

     <p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"   
        icon="ui-icon-arrowthichk-s">  
    <p:fileDownload value="#{fileDownloadController.file}" />  
</p:commandButton>  



 private StreamedContent file;  
 public StreamedContent getFile() {  
        return file;  
    }    
public void setFile(StreamedContent file){
 InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");  
        file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");  
}

如果你想从h:commandLink触发downloadPDF()监听器,你应该使用 actionListener 而不是 action 属性:

<h:commandLink id="downloadLink" value="Download PDF" target="_blank" actionListener="#{pc_ArchiveFiles.downloadPDF}" />
于 2012-09-28T18:06:41.277 回答