下面是一個使用action listener 的一個例子.
添加下面的代碼到backing bean的action listener中:
public?void?viewPdf(ActionEvent?event)?{ ?String?filename?=?"filename.pdf";
?//?use?your?own?method?that?reads?file?to?the?byte?array ?byte[]?pdf?=?getTheContentOfTheFile(filename);?
?FacesContext?faces?=?FacesContext.getCurrentInstance(); ?HttpServletResponse?response?=?(HttpServletResponse)?faces.getExternalContext().getResponse(); ?response.setContentType("application/pdf"); ?response.setContentLength(pdf.length); ?response.setHeader(?"Content-disposition",?"inline;?filename=\""+fileName+"\""); ?try?{ ??ServletOutputStream?out; ??out?=?response.getOutputStream(); ??out.write(pdf); ?}?catch?(IOException?e)?{ ??e.printStackTrace(); ?} ?faces.responseComplete(); } |