2013-08-20 45 views
3

我有一個按鈕,它打開一個帶有生成的pdf文件的新選項卡。 但是,當我點擊按鈕後,我想導航到另一個頁面。在新標籤頁/窗口中打開/下載文件時刷新/瀏覽當前頁面

這意味着,點擊按鈕後,我想用pdf打開一個新選項卡並導航到最初選項卡上的另一頁。我正在使用primefaces p:commandButton並嘗試onclick="window.location.href='www.google.de'",但它不起作用。但是onclick="window.lalert('www.google.de')"確實有效。

這是我的代碼:

<h:form id="transForm" target="_blank"> 
<p:commandButton value="Zertifikat erstellen" ajax="false" 
           label="Speichert die Anmeldung und erstellt ein Zertifikat im PDF-Format" 
           action="#{transportErfassen.generatePDFZertifikat()}"/> 

</h:form> 

generatePDFZertifikat()確實創建PDF文件與下面的代碼,我覺得這裏的問題:

FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ExternalContext externalContext = facesContext.getExternalContext(); 

    externalContext.setResponseContentType("application/pdf"); 
    externalContext.setResponseHeader("Expires", "0"); 
    externalContext.setResponseHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0"); 
    externalContext.setResponseHeader("Pragma", "public"); 
    externalContext.setResponseHeader("Content-disposition", "inline; filename=\"" + fileName +"\""); 
    externalContext.setResponseContentLength(out.length); 
    externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>()); 

    //setze explizit auf OK 
    externalContext.setResponseStatus(200);  

    OutputStream os = externalContext.getResponseOutputStream(); 
    os.write(out, 0, out.length); 
    os.flush(); 

    facesContext.responseComplete();  
    facesContext.renderResponse();  

回答

3

你基本上要發送2級響應回到1請求。這不會在HTTP中工作。如果你想發回2個回覆,你必須讓客戶以某種方式觸發2個請求。您已經在尋找解決方案的正確方向,只需很少的JavaScript幫助就可以在單個事件上觸發多個請求(單擊)。然而,您在onclick中的嘗試無效,在提交表單之前單擊提交按鈕,更改window.location,完全中止按鈕的原始操作,提交表單。

最好的辦法就是直接導航到結果頁面而這又調用JavaScript的window.open()在頁面加載,指着你想打開PDF文件的URL。這是不可能發送一些HTML/JS代碼連同PDF文件指示導航(因爲這顯然會破壞PDF文件)。這也意味着,您無法直接將PDF返回到表單提交請求。代碼必須重新設計,以便可以通過後續的GET請求來檢索PDF。最好的方法是使用一個簡單的servlet。您可以將生成的PDF臨時存儲在磁盤或會話中並與唯一密鑰關聯,並將該唯一密鑰作爲請求路徑信息或參數傳遞給window.open() URL中的servlet。

這裏的一個開球例如:

初始形式:

<h:form> 
    ... 
    <p:commandButton ... action="#{bean.submit}" /> 
</h:form> 

豆:

public String submit() { 
    File file = File.createTempFile("zertifikat", ".pdf", "/path/to/pdfs"); 
    this.filename = file.getName(); 

    // Write content to it. 

    return "targetview"; 
} 

目標視圖:

<h:outputScript rendered="#{not empty bean.filename}"> 
    window.open('#{request.contextPath}/pdfservlet/#{bean.filename}'); 
</h:outputScript> 

PDF的servlet(nullchecks等爲簡潔起見省略; Java 7中承擔了Files#copy()):

@WebServlet("/pdfservlet/*") 
public class PdfServlet extends HttpServlet { 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     File file = new File("/path/to/pdfs", request.getPathInfo().substring(1)); 
     response.setHeader("Content-Type", "application/pdf"); 
     response.setHeader("Content-Length", String.valueOf(file.length())); 
     response.setHeader("Content-Disposition", "inline; filename=\"zertifikat.pdf\""); 
     Files.copy(file.toPath(), response.getOutputStream()); 
    } 

} 
+0

感謝您的回覆,我真的很感激。 <3 – leostiw

+0

不客氣:) – BalusC

1

正如BalusC說,Refresh/navigate current pageopening downloading file是兩個不同的反應,必須有兩個resquests。我遇到了類似的問題。我用jsf ajax成功解決了它。

這裏是我的代碼部分:

XHTML:

<h:commandButton id="download-button" class="download-button" 
    value="download"> 
<f:ajax event="click" execute="@form" render=":msg-area" 
    listener="#{myController.checkForDownload}" onevent="checkCallBack" /> 
</h:commandButton> 
<h:commandButton id="download-button2" class="download-button2" 
    value="download" style="display: none;" 
    action="#{myController.download}"> 
</h:commandButton> 

的Javascript:

function checkCallBack(data) { 
    var ajaxStatus = data.status; 
    switch (ajaxStatus) { 
    case "begin": 
     break; 
    case "complete": 
     break; 
    case "success": 
     document.getElementById('download-form:download-button2').click(); 
     break; 
    } 
} 

download-button呈現頁面上的信息區和download-button2觸發下載方法。他們是兩個不同的要求。當第一個請求完成時,第二個請求將被觸發。

相關問題