需求:當(dāng)進(jìn)行文件長傳保存等操作時(shí),能在頁面顯示一個(gè)帶百分比的進(jìn)度條,給用戶一個(gè)好的交互體驗(yàn)
實(shí)現(xiàn)步驟
JSP頁面
1.添加table標(biāo)簽
table id="load" width="700" border="0" align="center" bgcolor="#FAFAFA" cellpadding="0" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse;display:none "> tr> td>br>br> table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#287BCE" style="border-collapse:collapse "> tr bgcolor="#F7F7F6"> td width="20%" height="100" valign="middle"> table align='center' width='500'> tr> td colspan='2' align='center' id="progressPersent">font size="2"> 正在進(jìn)行保存,用時(shí)較長,請稍后... /font> /td> /tr> tr> td id='tdOne' height='25' width=1 bgcolor="blue">nbsp;/td> td id='tdTwo' height='25' width=500 bgColor='#999999'>nbsp;/td> /tr> /table> /td> /tr> /table> /td> /tr> /table>
這個(gè)table標(biāo)簽要隱藏,進(jìn)度條執(zhí)行的時(shí)候再顯示。id為tdOne和tdTwo分別為進(jìn)度條的藍(lán)色和灰色區(qū)域。
2.添加js代碼
/**添加帶百分比的進(jìn)度條*/ var xmlHttp; //創(chuàng)建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秒鐘執(zhí)行一次percentServer()方法,直到當(dāng)前訪問結(jié)束 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數(shù)據(jù)中的percent存放的百分比的值 var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0].firstChild.data; var tdOne = document.getElementById("tdOne"); var progressPersent = document.getElementById("progressPersent"); //改變藍(lán)色區(qū)域的寬度 tdOne.width = percent_complete + "%"; //將百分比的數(shù)值顯示到頁面上 progressPersent.innerHTML = percent_complete + "%"; //如果計(jì)算獲取的百分比的數(shù)值沒有達(dá)到100,則繼續(xù)調(diào)用方法,直到操作結(jié)束為止 if (percent_complete 100) { setTimeout("percentServer()", 1000); } } } } function clearLoad() { document.getElementById("load").style.display=""; document.getElementById("opperate1").style.display="none"; document.getElementById("opperate2").style.display="none"; }
當(dāng)點(diǎn)擊保存時(shí),執(zhí)行l(wèi)oading()方法。
Action類
progressBar()方法
/** * @throws Exception * @Name: progressBar * @Description: 在頁面執(zhí)行保存后,使用ajax,計(jì)算執(zhí)行的百分比情況,將結(jié)果顯示到頁面上 * @Parameters: 無 * @Return: ajax如果不需要跳轉(zhuǎn)頁面,返回null或者NONE */ public String progressBar() throws Exception{ //從session中獲取操作方法中計(jì)算的百分比 Double percent = (Double) request.getSession().getAttribute("percent"); String res = ""; //此時(shí)說明操作的業(yè)務(wù)方法仍然繼續(xù)在執(zhí)行 if(percent!=null){ //計(jì)算的小數(shù),四舍五入取整 int percentInt = (int) Math.rint(percent); res = "percent>" + percentInt + "/percent>"; } //此時(shí)說明操作的業(yè)務(wù)方法已經(jīng)執(zhí)行完畢,session中的值已經(jīng)被清空 else{ //存放百分比 res = "percent>" + 100 + "/percent>"; } //定義ajax的返回結(jié)果是XML的形式 PrintWriter out = response.getWriter(); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); //存放結(jié)果數(shù)據(jù),例如:response>percent>88/percent>/response> out.println("response>"); out.println(res); out.println("/response>"); out.close(); return null; }
save()方法
/** * @Name: save * @Description: 保存表單數(shù)據(jù)到數(shù)據(jù)庫 * @Parameters: 無 * @Return: String:重定向到system/actingIndex.jsp再查詢 */ public String save(){ //模擬:循環(huán)保存150次,方便觀察百分比變化 for(int i=1;i=150;i++){ elecCommonMsgService.saveCommonMsg(elecCommonMsg);//保存數(shù)據(jù) request.getSession().setAttribute("percent", (double)i/150*100); } //線程結(jié)束,清空session request.getSession().removeAttribute("percent"); return "save"; }
注意:可以在復(fù)雜的業(yè)務(wù)中將代碼段分成點(diǎn),一個(gè)點(diǎn)的進(jìn)度是占百分之多少,然后存放到Session中,然后通過ajax調(diào)用服務(wù)從Session中獲取值,返回進(jìn)度并顯示即可。
效果
輸入數(shù)據(jù),點(diǎn)擊【保存】時(shí):
總結(jié)
帶百分比的進(jìn)度條,實(shí)際上是用ajax在保存中開啟多個(gè)線程實(shí)現(xiàn)的:
一個(gè)線程,執(zhí)行保存的操作:
1.將百分比計(jì)算出來,放到session中;
2.在線程結(jié)束的時(shí)候,將session清空。
另一個(gè)線程,從session中獲取百分比的內(nèi)容:
1.使用ajax將結(jié)果返回并顯示在頁面上
以上所述是小編給大家介紹的使用Ajax實(shí)現(xiàn)簡單的帶百分比進(jìn)度條實(shí)例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
標(biāo)簽:本溪 四川 營口 益陽 玉樹 遼寧 銅川 內(nèi)江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Ajax實(shí)現(xiàn)簡單的帶百分比進(jìn)度條實(shí)例》,本文關(guān)鍵詞 使用,Ajax,實(shí)現(xiàn),簡單,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。