【问题标题】:How reload jsf page after primefaces upload?primefaces上传后如何重新加载jsf页面?
【发布时间】:2013-08-25 15:32:48
【问题描述】:

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

还有其他方法可以做到这一点吗? 我想刷新一个&lt;a4j:repeat&gt;

【问题讨论】:

  • 嗯,不用“javascript:”试试。它应该能够执行重新加载。但是您可能会遇到使用错误 URL 重新加载页面的问题。如果您不使用 ?faces-redirect=true 进行导航,那么您的 URL 将始终落后一页

标签: javascript jsf file-upload jsf-2 primefaces


【解决方案1】:

&lt;p:fileUpload /&gt; 有一个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!

【讨论】:

    【解决方案2】:

    请尝试使用

    onsuccess="location.reload();"
    

    它对我很有效。在点击保存按钮以查看新上传的文件后,我正在使用它重新加载我的页面。当您使用更新所有表单时它不起作用 update="@(form)"

    【讨论】:

    【解决方案3】:

    请尝试在上传方法末尾添加:

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath()+"/faces/yourpage.xhtml");
    

    重新加载同一页面。对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 2011-08-19
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多