4 Replies Latest reply on Jun 23, 2009 10:27 AM by Ilya Shaikovsky

    issues using a4j:commandButton

    rajesha Indurthi Newbie

      I have a command button on my UI, the functionality of the button is to export some information in the zipped file format, so that the zip file can be saved on the local file system of the use. The below code snippet shows the backing bean code which does this for me .



      HttpServletResponse response = getServletResponse();
       ExportService service = new ExportService();
      
       try
       {
       // Init servlet response.
       response.reset();
       response.setContentType("application/x-zip-compressed");
       response.setHeader("Content-disposition", "attachment; filename=\"ic-tdp-export.zip\"");
       OutputStream os = response.getOutputStream();
       service.exportContent(rawList, os);
       os.close();
      
       }
       catch (IOException e)
       {
       error(logger, e);
       }
      
      
      
       // Prevent other JSF lifecycle phases eventually being invoked.
       // Otherwise you can get the following exception:
       // java.lang.IllegalStateException: Cannot forward after response has
       // been committed.
       FacesContext.getCurrentInstance().responseComplete();




      The code works perfectly fine when I use a simple h:commandButton, however I had to add some ajax capability to the button and so changed it to a a4j:commandButton, now when I click the buton instaed of exporting the data to a zip file as I expected, it is displaying me a blank page with the symbols PK . The exportContent service method is as shown below



      public void exportContent(final List<String> rawFiles, final OutputStream out)
       {
       int contentSize = 0;
       ZipOutputStream zipOutStream = null;
      
       ZipEntry zipEntry = null;
      
       zipOutStream = new ZipOutputStream(new BufferedOutputStream(out));
      
       try
       {
       for (int i = 0; i < rawFiles.size(); i++)
       {
       byte[] buf = rawFiles.get(i).getBytes();
       zipEntry = new ZipEntry("File_" + i);
       zipOutStream.putNextEntry(zipEntry);
       zipOutStream.write(buf);
       contentSize += buf.length;
       }
       /*int i = 0;
       for (Long raw : rawFiles)
       {
      
       System.out.println("In Srv " + raw.length());
       System.out.println("In Srv [" + raw + "]");
       in = new ByteArrayInputStream(raw.getBytes());
       zipEntry = new ZipEntry("rawFile" + i);
       zipOutStream.putNextEntry(zipEntry);
       int len;
       while ((len = in.read(buf)) > 0)
       {
       byte[] tempBuf = new byte[len];
       System.arraycopy(buf, 0, tempBuf, 0, len);
       zipOutStream.write(tempBuf);
       contentSize += len;
       }
       i = i + 1;
       in.close();
      
       }
       */
       setContentLength(contentSize);
       out.flush();
       }
       catch (Exception e)
       {
       e.printStackTrace();
       }
      



      As you can see from the above code,the raw files are set to the outputStream, which is of the zip file format . All this works perfectly fine with a simple h:commadbutton , but I replace it with a a4j commandbutton , it displays a blank page.