資料消極式載入,消極式載入
我們在上網的時候經常會看到這樣的效果,在我們發送請求之後,資料顯示在瀏覽器之前,會有一個圓圈在不斷地轉
今天比較好奇,就找了找資料研究了一下
lazyLoad.jsp
<%@ page language="java" pageEncoding="utf-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><script type="text/javascript"> function check(frm){ var m = document.getElementById("mFrame"); m.contentWindow.document.getElementById("div_loading").style.display = 'block'; m.contentWindow.document.getElementById("div_main").style.display = 'none'; return true; } </script></head><body> <div align="center"> <s:form action="searchFile" method="post" target="mFrame" theme="simple" onsubmit="return check(this)"> <table class="tab_frm" width="100%"> <tr> <td colspan="5" align="center"><s:submit value="查詢" cssClass="btn_normal" theme="simple" /> <s:reset value="重設" theme="simple" cssClass="btn_normal" /></td> </tr> </table> </s:form> </div> <iframe id="mFrame" name="mFrame" src="common/blank.html" frameborder="0" scrolling="auto" width="100%" height="100%" > </iframe> </body></html>mFrame的資料來源來自blank.html
其代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div align="center" id="div_loading" style=" display: none;"> <img src="../images/loading32.gif" /> <br /> <br /> <span style="font-size:15px;font-weight:normal;">資料讀取中,請稍後...</span> </div> <div align="center" id="div_main" style="display: block;"> <img src="../images/asdf.png" /> </div> </body></html>
loading32.gif如下
至於asdf.png是我隨便找的一張圖,lazyLoad.jsp的效果如下:
至於提交的請求searchFile,是一個struts的action
struts.xml如下
<action name="searchFile" class="cdm.module.file.action.SearchFileAction" method="lazyLoad"><result>/return.jsp</result></action>
對應的出來函數:
public String lazyLoad(){ try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; }返回的return.jsp就是一個最簡單的顯示
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>你看見我了嗎?</body></html>
最後的效果
點擊查詢5秒之內
5秒之後,就會顯示return.jsp的內容