Primefaces download file from datatable [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable" dir="ltr" rel="nofollow">How and when should I load the model from database for h:dataTable</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul>

I have this weird issue with File Download option not being called from the XHTML. I am very new to JSF, so it might be something basic that I am screwing up, but any help would be appreciated.

This is my XHTML

Files List

<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>

This is the backing bean

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


<img alt="JSF Page showing the download link" class="b-lazy" data-src="https://i.stack.imgur.com/EyELm.jpg" data-original="https://i.stack.imgur.com/EyELm.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

When I click on download, I want the downloadPDF() called. But it seems to be calling getArchiveFiles() method again instead of downloadPDF(). I have changed p:CommandLink to the below code, but it still does not call the right method. On top of it I have to pass in the filename parameter as well if I get this working.

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


<strong>value attribute</strong> for <strong>p:fileDownload</strong> should be an <strong>instance of StreamedContent</strong>. check in the documentation <a href="http://www.primefaces.org/documentation.html" rel="nofollow">here</a> on page 174 it should be something like code below:

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

If you want downloadPDF() <strong>listener</strong> fired from <strong>h:commandLink</strong> you should use actionListener instead of action attribute:

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


  • javax.el.PropretyNotWritableException: The class Article does not have a writable property 'id&
  • Why can't I inject 2 EJB's into 2 different managed beans that inject each other?
  • p:dataTable rowEdit doesn't update row object [duplicate]
  • Old values in input fields by GET request
  • JSF Trinidad tr:inputText trimming
  • /Registration.xhtml @19,81 validator=“#{UsernameValidator}”: Identity 'UsernameValidator'
  • JSF property transfer from backing bean A to backing bean B
  • JSF value expressions and beans, how do they work?
  • Commandlink action and @viewscoped weird behaviour
  • Servlet.service() for servlet Faces Servlet threw exception: java.lang.NullPointerException JSF
  • If I exclude all 64-bit libraries from my android app, will it run entirely in a 32-bit mode?
  • How to execute javascript method from server side?
  • Why “propdp” code snippet doesn't use the nameof operator for the name of the registered proper
  • Singleton Alternative - is it equivalent?
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • JPA CDI Injecting DAO into an Entity
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Display validation errors inside
  • Parsing a CSV string while ignoring commas inside the individual columns
  • debug library loaded with ctypes using gdb
  • MVC3 Razor - ListBox pre-select not working
  • how to adjust image in a panel in Java swing?
  • How do I change content of ComboFieldEditor?
  • Spring Data JPA custom method causing PropertyReferenceException
  • req.body is undefined - nodejs
  • output of program is not same as passed argument
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?