javascript 上传primefaces后如何重新加载jsf页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18388306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:43:52  来源:igfitidea点击:

How reload jsf page after primefaces upload?

javascriptjsffile-uploadjsf-2primefaces

提问by Kelson Victor

What a may to reload a jsf page after primefaces upload?

在primefaces上传后重新加载jsf页面有什么可能?

I try with javascript oncomplete="javascript:window.location.reload()", but it's doesn't work:

我尝试使用 javascript oncomplete="javascript:window.location.reload()",但它不起作用:

xhtml:

html:

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

There are another ways to do that? I want to refresh a <a4j:repeat>

还有其他方法可以做到这一点吗?我想刷新一个<a4j:repeat>

回答by Xtreme Biker

<p:fileUpload />has an updateattribute which, given @formvalue, renders the whole formagain.

<p:fileUpload />有一个update属性,给定@form值,再次呈现整个表单

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

Otherwise, if you want to get rid of ajax features, you also have the basic fileUploadcomponent.

否则,如果您想摆脱 ajax 功能,您还有基本的 fileUpload组件。

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

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

Basically, try not javascripting what is already done!

基本上,尽量不要对已经完成的事情使用javascript!

回答by Michal

Please, try to use

请尝试使用

onsuccess="location.reload();"

It works for me pretty well. I am using it to reload my page after hitting on save button to see new uploaded file. It would not work when you use updating all forms update="@(form)"

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