簡體   English   中英

p:commandButton不會打開帶有更新的窗口

[英]p:commandButton does not open window with update

我在我的Bean中生成一個exportList:

    public void exportExcel() throws WriteException {
    try {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=\"hours.xls\";");
        OutputStream out = response.getOutputStream();

        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("de", "DE"));
        WritableWorkbook workbook = Workbook.createWorkbook(out, ws);
        WritableSheet sheet = workbook.createSheet("Sheet1", 0);
        sheet.addCell(new Label(0, 0, "ID", bold));
        int row = 1;
        for (Hour hour : this.listHours) {
            sheet.addCell(new Label(0, row, String.valueOf(hour.getId())));
            row++;
        }

        SheetFormatter.setOptimalColumnWidth(sheet);
        workbook.write();
        workbook.close();
        response.flushBuffer();
        context.responseComplete();
        context.addMessage(null, new FacesMessage("Liste Exportiert"));
    }
    catch (Exception e) {
    }
}

在我的頁面中,我在p:commandButton中調用方法

    <p:commandButton  value="#{msg.export}" update="growl"
                    immediate="true" action="#{hoursView.exportExcel()}" />

我的頁面不會打開excel-List ...如果添加屬性ajax =“false”它可以工作但是更新將不會執行...

有關信息,我的Bean是SessionScoped,如果這會產生一些差異

您的第一個錯誤是您嘗試使用ajax下載文件。 那是不可能的。 Ajax由JavaScript代碼執行,由於安全原因,沒有設施強制“另存為”對話和/或將檢索到的響應寫入本地磁盤文件系統。 否則,這將打開各種令人討厭的安全漏洞可能性的大門。

因此,使用ajax="false"是絕對必要的。

您的第二個錯誤是您嘗試將不同的響應混合到一個響應中。 那是不可能的。 您只能返回文件下載或ajax更新,而不能同時返回兩者。 要檢索兩個不同的響應,您基本上需要讓客戶端發送兩個不同的請求。 您可以按如下方式處理:

  1. 讓客戶端向支持bean發送ajax請求。
  2. 讓服務器在服務器的臨時存儲系統中創建並保存Excel文件,並生成一個唯一的URL,以便servlet可以訪問它。
  3. 讓服務器發送包含消息和URL的ajax響應。
  4. 讓客戶端顯示消息並在URL上調用新的GET請求。 你可以使用window.location=url; 讓JavaScript在URL上調用新的GET請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM