[英]How reload jsf page after primefaces upload?
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>
<p:fileUpload />
has an update
attribute which, given @form
value, renders the whole form again. <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 fileUpload component.否则,如果您想摆脱 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!
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)"
时,它将不起作用
Please try to add at the end of upload method:请尝试在上传方法的末尾添加:
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath()+"/faces/yourpage.xhtml");
to reload the same page.重新加载同一页面。 For me it works fine.
对我来说它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.