title: Using a commandButton in a jsf Page to download a file
body: <p>Using a commandButton in a jsf Page to download a file. Using: JSF &amp; Richfaces.</p>
  
  <p>I have a table (extends ExtendedDataModel implements Modifiable, Serializable) with some data and in each row a button "download".</p>
  
  <pre><code>&lt;a4j:commandButton id="getDownload" value="download" 
      style="margin-left:10px;margin-right:10px;width:100px;"
      action="#{controller.download}" immediate="true" ajaxSingle="true"&gt;
      &lt;f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" /&gt;                     
  &lt;/a4j:commandButton&gt;
  </code></pre>
  
  <p>I have to build the file in the controller:</p>
  
  <pre><code>public void download(){
   OutputStream out = null;
  ....
  
  FacesContext fc = FacesContext.getCurrentInstance();
  HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
  out = response.getOutputStream();
  
  ZipOutputStream zipout = new ZipOutputStream(out);
  .....
  
  zipout.close();
  response.setContentType("application/octet-stream");
  response.addHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");
  out.flush();
  ....
  
  } finally {
      try {
          if (out!=null){
              out.close();
          }
          FacesContext.getCurrentInstance().responseComplete();
      } catch (IOException e) {
          logger.error(e);
      }
  
  }
  ...
  }
  </code></pre>
  
  <p>The Problem started, when I implemented the ExtendedDataModel my self.
  At first i used h:commandLink, but the controller method was never called... i tried and tried... now the correct method is called, but the (zip) file content is displayed in the page. I want a button/link in the page, which the user can click to download the file. The page itself should not change. Any ideas? </p>
  
  <p>I can create a servlet, but i do not understand why the ExtendedDataModel changed the behavior of the links inside.</p>
  
  <p><strong>Edit1</strong></p>
  
  <p>I used </p>
  
  <pre><code>&lt;h:commandLink id="getDownload" value="download" action="#{controller.download}"&gt;                                                       
                              &lt;f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" /&gt;                              
                          &lt;/h:commandLink&gt;
  </code></pre>
  
  <p>before. It works with the "normal" richfaces table, but not when i used it inside my own table which extends ExtendedDataModel. </p>
  
  <p><strong>Edit 2 - Solution/Workaround</strong></p>
  
  <p>It is impossible to use the h:commandButton, .. Link... whatever inside of the self made table, to download a file. I am now using one button in the table to render a new PanelGroup and a second button inside of the new PanelGroupt to download the file.
  I googled a lot for this, seems like a rich faces bug.</p>