请教一个使用 DWR 访问 servlet API 的问题

大家好,我在使用 DWR 做 EXCEL 导出的时候遇到一个问题。
大体描述一下:我要使用 DWR 结合 EXT 完成一个将后台数据导出到 EXCEL 功能,主要目的是增加一个导出进度提示,当用户提交导出数据请求时开始给出导出数据进度条提示,当响应结束时进度条隐藏,但是每次进行导出的时候都会报异常:
response committed.... available header

   我的代码如下:

java 代码
  1. package bss.rsccut.utils;   
  2.   
  3. import java.io.IOException;   
  4. import java.text.DateFormat;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7. import java.util.Iterator;   
  8. import java.util.List;   
  9. import java.util.Map;   
  10.   
  11. import javax.servlet.ServletContext;   
  12. import javax.servlet.ServletOutputStream;   
  13. import javax.servlet.http.HttpServletResponse;   
  14.   
  15. import jxl.Workbook;   
  16. import jxl.format.Colour;   
  17. import jxl.format.UnderlineStyle;   
  18. import jxl.write.Label;   
  19. import jxl.write.WritableCellFormat;   
  20. import jxl.write.WritableFont;   
  21. import jxl.write.WritableSheet;   
  22. import jxl.write.WritableWorkbook;   
  23. import jxl.write.WriteException;   
  24.   
  25. import org.apache.commons.logging.Log;   
  26. import org.apache.commons.logging.LogFactory;   
  27. import org.directwebremoting.WebContext;   
  28. import org.directwebremoting.WebContextFactory;   
  29.   
  30. import bss.common.DAOException;   
  31. import bss.common.util.SpringUtil;   
  32. import bss.rsccut.cuttask.dao.CutTaskDAO;   
  33.   
  34. /**  
  35.  * @author lsz  
  36.  *   
  37.  * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板  
  38.  */  
  39. public class PrintService{   
  40.     private static Log logger = LogFactory.getLog(PrintService.class);   
  41.   
  42.     private WebContext ctx;   
  43.   
  44.     private CutTaskDAO cutTaskDAO;   
  45.   
  46.     public PrintService() {   
  47.         this.ctx = WebContextFactory.get();   
  48.     }   
  49.   
  50.     private CutTaskDAO getCutTaskDAO(int areaId) {   
  51.         return (CutTaskDAO) SpringUtil.getBean(areaId, SpringUtil.RSC,   
  52.                 getServletContext(), "cutTaskDAO");   
  53.     }   
  54.   
  55.     private HttpServletResponse getResponse() {   
  56.         WebContext ctx = WebContextFactory.get();   
  57.         return ctx.getHttpServletResponse();   
  58.     }   
  59.        
  60.        
  61.   
  62.     private ServletContext getServletContext() {   
  63.         return ctx.getServletContext();   
  64.     }   
  65.   
  66.     public int export2Excel(Map map) throws IOException {   
  67.         logger.debug("-------go to PrintService.export2Excel-------");   
  68.         int rtCode = -1;   
  69.         HttpServletResponse response = getResponse();   
  70.         if (response == null) {   
  71.             logger.debug("----response is null--------");   
  72.             return -1;   
  73.         }   
  74. //      response.setStatus(HttpServletResponse.SC_ACCEPTED);   
  75.         logger.debug("--------response--------" + response);   
  76.         String taskId = "" + map.get("taskId");   
  77.         logger.debug("-----taskId-------" + taskId);   
  78.         String atomId = "" + map.get("atomId");   
  79.         logger.debug("----atomId------" + atomId);   
  80.         String targets = "" + map.get("targets");   
  81.         logger.debug("-------targets----" + targets);   
  82.         cutTaskDAO = getCutTaskDAO(1);   
  83.         logger.debug("-------cutTaskDAO------"+cutTaskDAO);   
  84.         try {   
  85.             if (targets.equals("0")) {   
  86.                 logger.debug("---------export all data--------");   
  87.                 map = cutTaskDAO.findAllPrintItems(taskId, atomId);   
  88.             } else {   
  89.                 logger.debug("----------export some data---------");   
  90.                 map = cutTaskDAO.findAppointedPrintItems(taskId, atomId,   
  91.                         targets);   
  92.             }   
  93.         } catch (DAOException e) {   
  94.             logger.debug("查询打印头信息异常,异常信息:" + e.getMessage());   
  95.             e.printStackTrace();   
  96.         }   
  97.         List headList = (List) map.get("headList");   
  98.         logger.debug("------headList-------\n"+headList);   
  99.         List itemList = (List) map.get("itemList");   
  100.         Date today = new Date();   
  101.         DateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");   
  102.         String fname = "task_" + taskId + "_rscinf_" + df.format(today);   
  103.         response.addHeader("content-disposition""attachment;filename="  
  104.                 + fname + ".xls");   
  105.         response.setContentType("application/vnd.ms-excel");   
  106.         ServletOutputStream outstream = response.getOutputStream();   
  107.         WritableWorkbook workbook = Workbook.createWorkbook(outstream);   
  108.         WritableSheet sheet = workbook.createSheet("工单信息"0);   
  109.         WritableFont wfc = null;   
  110.         WritableCellFormat wcfFC = null;   
  111.         //打印头   
  112.         WritableFont off = new WritableFont(WritableFont.ARIAL, 8,   
  113.                 WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,   
  114.                 Colour.BLUE);   
  115.         WritableFont nff = new WritableFont(WritableFont.ARIAL, 8,   
  116.                 WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,   
  117.                 Colour.RED);   
  118.         WritableCellFormat ocf = new WritableCellFormat(off);   
  119.         WritableCellFormat ncf = new WritableCellFormat(nff);   
  120.         wfc = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD, false,   
  121.                 UnderlineStyle.NO_UNDERLINE, Colour.BLACK);   
  122.         wcfFC = new WritableCellFormat(wfc);   
  123.         int pos = 0;   
  124.         try {   
  125.             for (Iterator it = headList.iterator(); it.hasNext();) {   
  126.                 Map head = (Map) it.next();   
  127.                 String flag = head.get("flag").toString();   
  128.                 String headId = head.get("headId").toString();   
  129.                 String headText = head.get("head").toString();   
  130.                 head.put("pos"new Integer(pos));   
  131.                 if (flag.equals("1")) {   
  132.                     Label label = new Label(pos, 0"原" + headText, ocf);   
  133.                     sheet.addCell(label);   
  134.                     pos++;   
  135.                     label = new Label(pos, 0"新" + headText, ncf);   
  136.                     sheet.addCell(label);   
  137.                     pos++;   
  138.                 } else {   
  139.                     Label label = new Label(pos, 0, headText, wcfFC);   
  140.                     sheet.addCell(label);   
  141.                     pos++;   
  142.                 }   
  143.             }   
  144.         } catch (WriteException e) {   
  145.             logger.debug("写EXCEL头异常,异常信息:" + e.getMessage());   
  146.             e.printStackTrace();   
  147.         }   
  148.         //打印记录   
  149.         nff = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD,   
  150.                 false, UnderlineStyle.NO_UNDERLINE, Colour.RED);   
  151.         ncf = new WritableCellFormat(nff);   
  152.         List subList = null;   
  153.         int nRow = 1;   
  154.         int nCol = 0;   
  155.         try {   
  156.             for (Iterator it = itemList.iterator(); it.hasNext();) {   
  157.                 subList = (List) it.next();   
  158.                 nCol = 0;   
  159.                 for (Iterator subIt = subList.iterator(); subIt.hasNext();) {   
  160.                     Map item = (Map) subIt.next();   
  161.                     int nFindPos = 0;   
  162.                     for (Iterator colIt = headList.iterator(); colIt.hasNext();) {   
  163.                         Map head = (Map) colIt.next();   
  164.                         if (head.get("headId").toString().equals(   
  165.                                 item.get("itemId").toString())) {   
  166.                             nFindPos = Integer.valueOf(   
  167.                                     head.get("pos").toString()).intValue();   
  168.                             break;   
  169.                         }   
  170.                     }   
  171.                     String oldValue = item.get("oldValue").toString();   
  172.                     Label label = new Label(nFindPos, nRow, oldValue);   
  173.   
  174.                     sheet.addCell(label);   
  175.                     if (item.get("newFlag").toString().equals("1")) {   
  176.                         String newValue = item.get("newValue").toString();   
  177.                         label = new Label(nFindPos + 1, nRow, newValue, ncf);   
  178.                         sheet.addCell(label);   
  179.                     }   
  180.                 }   
  181.                 nRow++;   
  182.             }   
  183.             workbook.write();   
  184.             workbook.close();   
  185.             rtCode = 1;   
  186.         } catch (WriteException e) {   
  187.             logger.debug("写EXCEL内容异常,异常信息:" + e.getMessage());   
  188.             e.printStackTrace();   
  189.         }   
  190.         return rtCode;   
  191.     }   
  192. }  

    js 代码

  1. <input type="hiden" id="taskId">   
  2. <input type="button"  id="exportBtn" value="导出">   
  3.   
  4. function __printOrder(atom){   
  5.    var taskId = $('taskOps').value;   
  6.    if(!taskId)   
  7.    {   
  8.       Ext.Msg.alert('操作提示', '请选择割接任务');   
  9.       disabledBtns(false);   
  10.       return;   
  11.    }   
  12.    var params = {taskId:taskId,atomId:atom,targets:'0'};   
  13.    printService.export2Excel(params,{   
  14.      callback:function(data){   
  15.        if(data == 1){   
  16.          Ext.Msg.alert('操作提示','导出成功');     
  17.        }else{   
  18.          Ext.Msg.alert('操作提示','导出异常,请查看日志');       
  19.        }   
  20.      },   
  21.      timeout:120000,   
  22.      exceptionHandler:function(message){   
  23.                            Ext.Msg.hide();   
  24.                            Ext.Msg.alert('异常','导出异常,异常信息:'+message);   
  25.                        },   
  26.      httpMethod:'POST',   
  27.      rpcType:DWREngine.XMLHttpRequest,   
  28.      preHook:function(){   
  29.                            Ext.Msg.show({   
  30.                                  title:'请稍侯',   
  31.                                  msg:'正在导出数据...',   
  32.                                  closable:false  
  33.                                });   
  34.                        },   
  35.      postHook:function(){Ext.Msg.hide();}   
  36.    });   
  37. }   
  38.   
  39. Ext.get('exportBtn').on('click',function(){   
  40.     __printOrder('103');   
  41. });  
哪位大侠有使用DWR访问servlet API的经验或者比较好的解决方法,望不吝赐教,不甚感激!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值