Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two problems.</p> <p>The first problem is that the <code>&lt;p:commandButton&gt;</code> sends by default an Ajax request. This request is fired by JavaScript code. However, JavaScript can't do anything with a response which contains a file download. Due to security restrictions JavaScript can't spawn a <em>Save As</em> dialogue or something. The response is basically totally ignored. </p> <p>You need to add <code>ajax="false"</code> to <code>&lt;p:commandButton&gt;</code> to turn ajax off so that the button fires a normal synchronous HTTP request, or you need to replace it by standard <code>&lt;h:commandButton&gt;</code>.</p> <pre class="lang-html prettyprint-override"><code>&lt;p:commandButton ajax="false" ... /&gt; </code></pre> <p>or</p> <pre class="lang-html prettyprint-override"><code>&lt;h:commandButton ... /&gt; </code></pre> <p>The second problem is that your servlet doesn't write the Excel file to the response at all, but instead to a local file which is stored in server's working directory. Basically, the HTTP response contains <em>nothing</em>. You need to pass <code>HttpServletResponse#getOutputStream()</code> to the <code>WorkBook#write()</code> method.</p> <pre><code>workbook.write(response.getOutputStream()); </code></pre> <p>On an unrelated note, I wonder how the servlet is useful here. Do you want to reuse it outside JSF? If not, you don't necessarily need to dispatch to the servlet at all, but just execute the same code in bean's action method. That empty <code>catch</code> block is also not nice. I'd just declare it as <code>throws</code> in method or at least rethrow it as <code>new FacesException(e)</code>.</p> <hr> <p><strong>Update</strong> as per the comments you seem not be interested in the servlet at all. Here's a minor rewrite how you could send the Excel file programmatically in a JSF action method.</p> <pre><code>public void getReportData() throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue(0.0); FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.xls\""); workbook.write(externalContext.getResponseOutputStream()); facesContext.responseComplete(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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