2013-08-22 45 views
5

在primefaces上傳後,可能會重新載入jsf頁面?primefaces上傳後如何重新加載jsf頁面?

我試着用JavaScript oncomplete="javascript:window.location.reload()",但它不工作:

XHTML:

<h:form> 
    <p:messages showDetail="true"/> 
    <p:fileUpload fileUploadListener="#{revistauploadBean.upload}" 
        mode="advanced" dragDropSupport="false" merge="true" 
        uploadLabel="Enviar" cancelLabel="Cancelar" 
        oncomplete="javascript:window.location.reload()" 
        label="Procurar PDF" sizeLimit="1000000000" fileLimit="3" 
        allowTypes="/(\.|\/)(pdf)$/" /> 
</h:form> 

豆:

public void upload(FileUploadEvent event) throws SQLException { 
    LoginAuthentication user = new LoginAuthentication(); 
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() 
     + " is uploaded."); 
    FacesContext.getCurrentInstance().addMessage(null, msg); 
    // Do what you want with the file   
    try { 
     copyFile(event.getFile().getFileName(), event.getFile().getInputstream()); 
     ConectaBanco mysql = new ConectaBanco(); 
     mysql.insere("INSERT INTO edicoes VALUES (NULL, '" 
      + mysql.pegaIDrev(user.getNome()) 
      + "', NULL, NULL, NULL, NULL, '" 
      + event.getFile().getFileName() 
      + "');"); 
     File pasta = new File(caminhoPastaPDF 
      + "/convertidos/" 
      + event.getFile().getFileName()); 
     pasta.mkdir(); 
     convertePdf(event.getFile().getFileName()); 
     FacesMessage msg1 = new FacesMessage("Succesful", event.getFile().getFileName() 
      + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, msg1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

public void copyFile(String fileName, InputStream in) { 
    try { 
     // write the inputStream to a FileOutputStream 
     OutputStream out = new FileOutputStream(new File(destination + fileName)); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 
     while ((read = in.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     in.close(); 
     out.flush(); 
     out.close(); 
     ////System.out.println("New file created!"); 
    } 
} 

有另一種方式來做到這一點? 我想刷新<a4j:repeat>

+0

uumm,請嘗試不使用「javascript:」。它應該能夠執行重新加載。但是您可能會遇到問題,您將使用錯誤的URL重新加載頁面。如果您不使用?faces-redirect = true進行導航,那麼您的網址總是會落後一頁 – noone

回答

2

<p:fileUpload />有一個update屬性其中,由於@form值,呈現整個窗體一次。

<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" 
    mode="advanced" dragDropSupport="false" 
    update="@form" sizeLimit="100000" fileLimit="3" 
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" /> 

否則,如果你想擺脫的Ajax特性,你也有basic fileUpload的組成部分。

<p:fileUpload value="#{fileUploadController.file}" mode="simple"/> 

<p:commandButton value="Submit" ajax="false" 
      actionListener="#{fileUploadController.upload}"/> 

基本上,嘗試不javascripting什麼已經完成!

0

請儘量使用

onsuccess="location.reload();" 

它爲我很好。點擊保存按鈕後,我使用它來重新加載我的頁面,以查看新上傳的文件。當您使用更新所有表格時,它不起作用 update="@(form)"

相關問題