Execute backing bean action on load?

后端 未结 4 1469
盖世英雄少女心
盖世英雄少女心 2020-12-14 12:19

I would like to build a results page for a report export page. This results page must display the status of the export and offer the download of this export.

The exp

4条回答
  •  独厮守ぢ
    2020-12-14 12:57

    You can also solve this in JSF 2.0 using component system events, specifically the PreRenderViewEvent.

    Just create a download view (/download.xhtml) that fires a download listener before render.

    
    
        
    
    

    Then, in your report bean (defined using JSR-299), you push the file and mark the response as complete.

    public @Named @RequestScoped class ReportBean {
    
       public void download() throws Exception {
          FacesContext ctx = FacesContext.getCurrentInstance();
          pushFile(
               ctx.getExternalContext(),
               "/path/to/a/pdf/file.pdf",
               "file.pdf"
          ); 
          ctx.responseComplete();
       }
    
       private void pushFile(ExternalContext extCtx,
             String fileName, String displayName) throws IOException {
          File f = new File(fileName);
          int length = 0; 
          OutputStream os = extCtx.getResponseOutputStream();
          String mimetype = extCtx.getMimeType(fileName);
    
          extCtx.setResponseContentType(
             (mimetype != null) ? mimetype : "application/octet-stream");
          extCtx.setResponseContentLength((int) f.length());
          extCtx.setResponseHeader("Content-Disposition",
             "attachment; filename=\"" + displayName + "\"");
    
          // Stream to the requester.
          byte[] bbuf = new byte[1024];
          DataInputStream in = new DataInputStream(new FileInputStream(f));
    
          while ((in != null) && ((length = in.read(bbuf)) != -1)) {
             os.write(bbuf, 0, length);
          }  
    
          in.close();
       }
    }
    

    That's all there is to it!

    You can either link to the download page (/download.jsf), or use a HTML meta tag to redirect to it on a splash page.

提交回复
热议问题