[導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示
一:功能要求
頁面進行ajax提交的時候,在服務端未響應的時間內(nèi),頁面出現(xiàn)“正在加載”的文字提示,告訴用戶他的請求已得到響應,得到服務端響應后,提示關閉不顯示。
二:代碼
1.首先是web頁面:
當點擊ID未span_1的文字后。發(fā)送一個get請求。
2.服務端
服務端是一個servlet,隨即顯示一段文字。
頁面進行ajax提交的時候,在服務端未響應的時間內(nèi),頁面出現(xiàn)“正在加載”的文字提示,告訴用戶他的請求已得到響應,得到服務端響應后,提示關閉不顯示。
二:代碼
1.首先是web頁面:
當點擊ID未span_1的文字后。發(fā)送一個get請求。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Loading demo</title> <script src="jquery-1.2.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("#span_1").click(function() { //alert("click"); $.get("Faq",{id:1}, function(xml){ var text = $("Response", xml).text(); $("#div_1").html(text); }); }); $("#loading").ajaxStart(function(){ //alert("start"); //$(this).html("<img src=loading.gif> 正在加載"); $(this).css("display", "block"); }); $("#loading").ajaxSuccess(function(){ //alert("end"); //$(this).html("加載完成!"); $(this).css("display", "none"); }); }); </script> </head> <body> <div id="faq"> <span id="span_1">什么是jquery?</span> <div id="div_1"></div> </div> <div id="loading" style="display:none"><img src=loading.gif> 正在加載</div> </body> </html> |
2.服務端
服務端是一個servlet,隨即顯示一段文字。
package com.hexin.ajax; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Faq extends HttpServlet { private final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; /** * Constructor of the object. */ public Faq() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("開始執(zhí)行"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF-8");//設置utf-8j解決亂碼問題 PrintWriter out = response.getWriter(); out.print(XML_HEAD); Random random = new Random(System.currentTimeMillis()); if(random.nextBoolean()){ out.println("<Response>論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。</Response>"); System.out.println("至個人信息管理中心可以進行基本信息管理、社區(qū)管理與論壇管理,可以查看短消息、整理個人收藏、查看個人主題,直接點擊頁面右側(cè)相關鏈接即可。"); }else{ out.println("<Response>論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。</Response>"); System.out.println("論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。"); } out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } |
文章來源:http://huxiaofei590.blog.163.com/blog/static/32596122007111441412585
posted on 2007-12-14 16:14 ThinkInJava 閱讀(4422) 評論(4) 編輯 收藏