2013-08-22 749 views
5

Điều gì có thể tải lại trang jsf sau khi tải lên các vị trí chính?Cách tải lại trang jsf sau khi tải lên các vị trí chính?

tôi cố gắng với javascript oncomplete="javascript:window.location.reload()", nhưng nó không hoạt động:

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> 

Bean:

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!"); 
    } 
} 

Có một cách để làm điều đó ? Tôi muốn làm mới một <a4j:repeat>

+0

uumm, dùng thử mà không có "javascript:". Nó sẽ có thể thực hiện tải lại. NHƯNG có thể bạn sẽ gặp sự cố khi tải lại trang có URL sai. Nếu bạn không điều hướng với? Faces-redirect = true thì URL của bạn sẽ luôn bị tụt hậu bởi một trang – noone

Trả lời

2

<p:fileUpload /> có một thuộc tính update đó, được đưa ra @form giá trị, ám chỉ rằng toàn bộ hình thức một lần nữa.

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

Ngược lại, nếu bạn muốn thoát khỏi tính năng ajax, bạn cũng có thành phần basic fileUpload.

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

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

Về cơ bản, hãy thử không javascript những gì đã được thực hiện!

0

Xin vui lòng, cố gắng sử dụng

onsuccess="location.reload();" 

Nó hoạt động đối với tôi khá tốt. Tôi đang sử dụng nó để tải lại trang của mình sau khi nhấn vào nút lưu để xem tệp mới được tải lên. Tính năng này sẽ không hoạt động khi bạn sử dụng cập nhật tất cả các biểu mẫu update="@(form)"