如何强制页面重新加载在primefaces?

时间:2014-03-12 14:26:59

标签: java javascript primefaces

我想在下载文件后强制在页面中重新加载页面。

我尝试了这个但是没有用

  <p:commandButton ajax="false" actionListener="#{fileDownloadController.forceDownload}">
    <p:fileDownload value="#{fileDownloadController.downloadXMLFile(myBean.mostRecentFile)}" />  
    </p:commandButton> 
 </p:commandButton> 

EDITED       此解决方案正确地重新加载,但它在文件可以下载之前发生

 public StreamedContent downloadXMLFile(DMFile dmFile) {
  ...
  return new DefaultStreamedContent(stream, "text/xml", fileName);

}

并且

public void forceReload(){
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    try {
        ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
    } catch (IOException ex) {
        Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

// Redirect
public static void redirect(String urlStr) 
{
    FacesContext ctx = FacesContext.getCurrentInstance();
    String url = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, urlStr.replaceAll("\\?faces-redirect=true", "")));
    try { ctx.getExternalContext().redirect(url); }
    catch (IOException ioe) { throw new FacesException(ioe); }
}

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());