Note that there are some explanatory texts on larger screens.

plurals
  1. POh:commandLink not firing file download action without f:ajax
    text
    copied!<p>I have an <code>h:datatable</code> that contains a column with a link that will call the backing bean and "should" return a PDF document generated.</p> <p>The column looks like this : </p> <pre><code>&lt;h:form&gt; ... &lt;h:datatable&gt; ... &lt;h:column &gt; &lt;h:commandLink action="#{bean.downloadPDF}" target="_blank" &gt; &lt;f:param name="value1" value="#{bean.val1}"/&gt; &lt;f:param name="value2" value="#{bean.val2}"/&gt; &lt;f:param name="value3" value="#{bean.val3}"/&gt; &lt;h:graphicImage name="certificate.jpg" library="images"/&gt; &lt;/h:commandLink&gt; &lt;/h:column&gt; ... &lt;/h:datatable&gt; ... &lt;/h:form&gt; </code></pre> <p>I have no javascript errors on my page (according to chrome and firebug). The backing bean looks like this : </p> <pre><code>public void downloadPDF() { ... File outputPDF = new File(outputFileName); //Get ready to return pdf to user BufferedInputStream input = null; BufferedOutputStream output = null; try { // Open file. input = new BufferedInputStream(new FileInputStream(outputPDF), 10240); //Return PDF to user // Init servlet response. response.reset(); response.setHeader("Content-Type", "application/pdf"); response.setHeader("Content-Length", String.valueOf(outputPDF.length())); response.setHeader("Content-Disposition", "inline; filename=\"" + pdfName + "\""); output = new BufferedOutputStream(response.getOutputStream(), 10240); // Write file contents to response. byte[] buffer = new byte[10240]; int length; while ((length = input.read(buffer)) &gt; 0) { output.write(buffer, 0, length); } // Finalize task. output.flush(); } catch(IOException e) { e.printStackTrace(); } finally { output.close(); input.close(); } facesContext.responseComplete(); } </code></pre> <p>Now, I know my PDF generation works because I can create the custom PDF and save it on the drive.</p> <p>Then, if I add <code>&lt;f:ajax/&gt;</code> to the <code>h:commandLink</code> The method is called. Without that, it reloads the current page without the action method ever being called. </p> <p>I have tried a few different things... with or without any <code>f:param</code>s. Adding a <code>String</code> return value to the <code>downloadPDF()</code> function. <strong>EDIT</strong> : Taking away the <code>target="_blank"</code> parameter. Using <code>actionListener</code> instead of <code>action</code>. When using <code>action</code>, the bean method does not get called, but I get this error message in the <code>h:messages</code> : <code>Conversion Error setting value '' for 'null Converter'.</code>.</p> <p>Cannot get it to call the function. What I am hoping to get is that when one clicks on the downloadPDF link, it opens a new window and downloads the PDF.</p> <p>Any help would be greatly appreciated.</p> <h3>EDIT 2 / Solution (temporary)</h3> <p>I have managed to get this working using the attribute <code>immediate="true"</code>. Still havn't found exact field that is giving me the <code>Conversion Error</code>, but am assuming is a field in the form that does not need to be submitted for this specific function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload