Thking In Java

关注应用程序的HA、可扩展性 多实践,遇到问题查doc,google,上论坛咨询

 

[导入]用JQuery实现ajax提交时页面显示正在提交的提示

一:功能要求
页面进行ajax提交的时候,在服务端未响应的时间内,页面出现“正在加载”的文字提示,告诉用户他的请求已得到响应,得到服务端响应后,提示关闭不显示。
二:代码
1.首先是web页面:
 当点击ID未span_1的文字后。发送一个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>&nbsp;正在加载");   
            $(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>&nbsp;正在加载</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("开始执行");
        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>论坛版主用户可以根据自己的特长喜好自愿申请,申请版主的用户应该是累积经验值达到1000分,威望值达到20以上的注册用户。版主需要承担相应的版面发展职责,并愿意为其他用户服务。</Response>");
            System.out.println("至个人信息管理中心可以进行基本信息管理、社区管理与论坛管理,可以查看短消息、整理个人收藏、查看个人主题,直接点击页面右侧相关链接即可。");
        }else{
            out.println("<Response>论坛版主用户可以根据自己的特长喜好自愿申请,申请版主的用户应该是累积经验值达到1000分,威望值达到20以上的注册用户。版主需要承担相应的版面发展职责,并愿意为其他用户服务。</Response>");
            System.out.println("论坛版主用户可以根据自己的特长喜好自愿申请,申请版主的用户应该是累积经验值达到1000分,威望值达到20以上的注册用户。版主需要承担相应的版面发展职责,并愿意为其他用户服务。");
        }
        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 阅读(4394) 评论(4)  编辑  收藏

评论

# re: [导入]用JQuery实现ajax提交时页面显示正在提交的提示 2014-06-26 10:44 ss

ssss  回复  更多评论   

# re: [导入]用JQuery实现ajax提交时页面显示正在提交的提示 2014-06-26 10:44 ss

cccccccccccccccccc  回复  更多评论   

# re: [导入]用JQuery实现ajax提交时页面显示正在提交的提示 2015-05-04 10:09 qw

aad  回复  更多评论   

# re: [导入]用JQuery实现ajax提交时页面显示正在提交的提示 2016-08-02 20:11

ddf  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

java

友情链接

搜索

最新评论