下面是一个使用action listener 的一个例子.
添加下面的代码到backing bean的action listener中:
添加下面的代码到backing bean的action listener中:
public void viewPdf(ActionEvent event) { |
下面是JSP代码:
<h:commandButton immediate="true" actionListener="#{backingBean.viewPdf}" value="Read PDF" />
缺点是:当出发这个事件下载一次以后,再去点页面上的选择按钮或者submmit时候会字段再次跳出下载画面。
解决办法是不用commandLink而改用commandButton。至于这里为什么要把方法放到action listener 中不太明白
我是把他放到了action中也可以。
代码
public String downloadFile(){
//生成文件
Writer osw = null;
String file="";
try{
file=bof.utils.Util.getTempFileName()+".xls";
OutputStream os=new FileOutputStream(init.base+"tmp"+File.separatorChar+file);
BufferedOutputStream bos=new BufferedOutputStream(os,65536);
osw =new OutputStreamWriter(bos);
osw.write("数量"+"/t");
osw.write("合同号"+"/t");
osw.write("合同日期"+"/t");
osw.write("交货日期"+"/t");
osw.write("销售员"+"/t");
osw.write("物料"+"/t");
osw.write("厂商价格"+"/t");
osw.write("合同数"+"/t");
osw.write("已申请"+"/t");
osw.write("下单"+"/t");
osw.write("入库"+"/t");
osw.write("销售"+"/t");
osw.write("未清"+"/t");
osw.write("/n");
//写入list数据
for (int i=0;i<selectedContractsInfo.size();i++) {
ContractInfo contract = (ContractInfo)selectedContractsInfo.get(i);
if(contract.isMarkedForGenerate()){
osw.write(contract.getNewRequest()+"/t");
osw.write(contract.getCoNumber()+"/t");
osw.write(contract.getSignDate()+"/t");
osw.write(contract.getDeliveryDate()+"/t");
osw.write(contract.getItcode()+"/t");
osw.write(contract.getItemName()+"/t");
osw.write((contract.getFactoryPrice()==null ? "" : contract.getFactoryPrice()) +"/t");
osw.write(contract.getItemQuantity()+"/t");
osw.write(contract.getRequestCount()+"/t");
osw.write(contract.getPurchaseCount()+"/t");
osw.write(contract.getInstockCount()+"/t");
osw.write(contract.getSaleCount()+"/t");
osw.write(contract.getUnDelivered()+"/t");
osw.write("/n");
osw.flush();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
osw.close();
}catch(Exception e){
e.printStackTrace();
}
}
//下载文件
String filepath=bof.init.base+"tmp"+java.io.File.separatorChar+file;
FacesContext ctx = FacesContext.getCurrentInstance();
ResponseWriter writer = ctx.getResponseWriter();
HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=/"" + file + "/"");
try{
// java.io.InputStream fileInputStream =new java.io.BufferedInputStream(new java.io.FileInputStream(filepath));
// int i;
// while ((i=fileInputStream.read()) != -1) {
// writer.write(i);
// }
// fileInputStream.close();
// writer.close();
ServletOutputStream os = response.getOutputStream();
FileInputStream fis = new java.io.FileInputStream(filepath);
byte[] b = new byte[1024];
int i = 0;
while((i=fis.read(b))>0){
os.write(b,0,i);
}
fis.close();
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
ctx.responseComplete();
return null;
}
jsp页面
<h:commandButton action="#{contractsInfo.downloadFile}" image="../skins/i2-standard/images/sav_as_actv.gif" />