jsp:
<script language="javascript"> function checkchar(){ if(document.Form2.stationRun.value.length>2500){ alert("站点运行情况字数不能超过2500字"); return; } if(document.Form2.devRun.value.length>2500){ alert("设备运行情况字数不能超过2500字"); return; } document.Form2.action="system/elecCommonMsgAction_save.do"; document.Form2.submit(); loading(); } function addEnter(element){ document.getElementById(element).value = document.getElementById(element).value+"<br>"; } function checkTextAreaLen(){ var stationRun = new Bs_LimitedTextarea('stationRun', 2500); stationRun.infolineCssStyle = "font-family:arial; font-size:11px; color:gray;"; stationRun.draw(); var devRun = new Bs_LimitedTextarea('devRun', 2500); devRun.infolineCssStyle = "font-family:arial; font-size:11px; color:gray;"; devRun.draw(); } window.οnlοad=function(){ //checkTextAreaLen(); } /**添加百分比的进度条显示*/ var xmlHttp; //创建ajax引擎 function createXMLHttpRequest() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e1) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { } } } } function loading() { createXMLHttpRequest(); clearLoad(); var url = "elecCommonMsgAction_progressBar.do"; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = createCallback; xmlHttp.send(null); } function createCallback() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { //每隔1秒钟执行一次percentServer()方法,直到当前访问结束 setTimeout("percentServer()", 1000); } } } function percentServer() { createXMLHttpRequest(); var url = "elecCommonMsgAction_progressBar.do"; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = updateCallback; xmlHttp.send(null); } function updateCallback() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { //获取XML数据中的percent存放的百分比的值 //此时xmlHttp.responseXML=<response><percent>15</percent></response> var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0].firstChild.data; var tdOne = document.getElementById("tdOne"); var progressPersent = document.getElementById("progressPersent"); //改变蓝色区域的宽度 tdOne.width = percent_complete + "%"; //将百分比的数值显示到页面上 progressPersent.innerHTML = percent_complete + "%"; //如果计算获取的百分比的数值没有达到100,则继续调用方法,直到操作结束为止 if (percent_complete < 100) { setTimeout("percentServer()", 1000); } } } } function clearLoad() { document.getElementById("load").style.display=""; document.getElementById("opperate1").style.display="none";//将现有的div隐藏 document.getElementById("opperate2").style.display="none";//将现有的div隐藏 } </script> <body> <table id="load" width="700" border="0" align="center" bgcolor="#FAFAFA" cellpadding="0" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse;display:none "> </body> |
controller:
@RequestMapper("save.do") public String save(){ //elecCommonMsgService.saveElecCommonMsg(elecCommonMsg);//测试百分比的进度条 //模拟:循环遍历150条数据,观察百分比的变化情况 for(int i=1;i<=150;i++){ elecCommonMsgService.saveElecCommonMsg(elecCommonMsg); request.getSession().setAttribute("percent", (double)i/150*100);//存放计算的百分比 } //线程结束时,清空当前session request.getSession().removeAttribute("percent"); return "save"; } @RequestMapper("elecCommonMsgAction_save.do") public String progressBar() throws Exception{ //从session中获取操作方法中计算的百分比 Double percent = (Double) request.getSession().getAttribute("percent"); String res = ""; //此时说明操作的业务方法仍然继续在执行 if(percent!=null){ //计算的小数,四舍五入取整 int percentInt = (int) Math.rint(percent); res = "<percent>" + percentInt + "</percent>"; } //此时说明操作的业务方法已经执行完毕,session中的值已经被清空 else{ //存放百分比 res = "<percent>" + 100 + "</percent>"; } //定义ajax的返回结果是XML的形式 PrintWriter out = response.getWriter(); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); //存放结果数据,例如:<response><percent>88</percent></response> out.println("<response>"); out.println(res); out.println("</response>"); out.close(); return null; } |