Pobierz plik za pomocą JSF

każdy!

Mam kłopot. Próbowałem zapisać plik Excela w aplikacji internetowej jsf. Wygenerowałem plik według moich narzędzi i próbowałem uzyskać okno „zapisz”, ale nie udało mi się.

Oto mój kod:

  <div>
  <h:commandButton value="Apply" actionListener="#{hornPhonesBean.generateReport}"/>
  </div>

i:

   public void generateReport(ActionEvent event) {
    System.out.println("GENERATE REPORT FROM = " + this.dateFrom + "; TO = " + this.dateTo);

    try {
        XSSFWorkbook workbook = (XSSFWorkbook) HornReportGenerator.getWorkbook(null, null);
        String fileName = "1.xlsx";

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
        ec.responseReset(); 

        // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
        ec.setResponseContentType("application/vnd.ms-excel"); 

        // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
        //ec.setResponseContentLength(contentLength); 

        // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 

        OutputStream output = ec.getResponseOutputStream();
        workbook.write(output); 
        output.flush();
        output.close();

        fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
        System.out.println("END");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Przeczytałem sugestie tutaj i na innych forach - wszyscy mówią, że nie powinienem ich używać, ale w ogóle z nich nie korzystałem.

Wtedy pomyślałem, że problem może być w

 <ice:form>, 

gdzie trzymałem

 <h:commandButton>, 

i zmieniłam na

 <h:form>, 

ale to nie pomogło.

Może problem w żądaniu - ma nagłówek Faces-Request częściowy / ajax. Ale nie jestem pewien.

Daj mi kilka pomysłów - spędziłem już 4 godziny na ten szalony problem z pobieraniem jsf)

questionAnswers(1)

yourAnswerToTheQuestion