日期:2014-05-16  浏览次数:20593 次

一个简单Ajax分页的实例

1.由前端页面发送Ajax请求至服务器端.
   
代码
        

 function QueryList(no){    
            var url="getXML.do";    
            var pars="pageNo="+no;    
            var ajax=new Ajax.Request(url,{    
            method:'get',    
            parameters:pars,    
            onComplete:showResponse    
            });    

?  ??
< type="text/javascript">render_code();
   

代码
?

  //回调    
   function showResponse(request){    
   
var parser=new MyXmlParser();    
parser.load(request);    
var dataList=parser.itemList;    
        var pageList=parser.pageList;    
        showTable(dataList,pageList);    
   
< type="text/javascript">render_code(); 
   这里用到的是prototype-1.4.0.js中的Ajax对象. 
  2.服务器端处理程序到数据库查询数据并生成xml档返回. 
   
代码
    StringBuffer resultXML = new StringBuffer();    
        int pageNo = new Integer(request.getParameter("pageNo")).intValue();    
            
        try {    
            resultXML.append("<?xml version='1.0' encoding='gb2312'?>");    
            resultXML.append("<ajax-response>\n");    
            resultXML.append("<root>\n");    
   
            Pagination page = new Pagination(request, 100);    
            List list = this.getTestList(page.getStart(), page.getPageSize());    
            for (int i = 0; i < list.size(); i++) {    
                TestBean bean = (TestBean) list.get(i);    
                resultXML.append("<data>\n");    
                resultXML    
                        .append("\t<id>" + bean.getId().toString() + "</id>\n");    
                resultXML.append("\t<name>" + bean.getName() + "</name>\n");    
                resultXML.append("\t<sex>" + bean.getSex() + "</sex>\n");    
                resultXML.append("\t<age>" + bean.getAge().toString()    
                        + "</age>\n");    
                resultXML.append("</data>\n");    
            }    
   
            resultXML.append("<pagination>\n");    
            resultXML.append("\t<total>" + page.getTotal() + "</total>\n");    
            resultXML.append("\t<start>" + page.getStart() + "</start>\n");    
            resultXML.append("\t<end>" + page.getEnd() + "</end>\n");    
            resultXML.append("\t<pageno>" + page.getPageNo() + "</pageno>\n");    
            resultXML.append("</pagination>\n");    
   
            resultXML.append("</root>\n");    
            resultXML.append("</ajax-response>");    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
        writeResponse(request, response, resultXML.toString());    
   

?  ??
< type="text/javascript">render_code();
3.在Ajax响应方法中对xml解析并生成表格显示到页面.


代码

 StringBuffer resultXML = new StringBuffer();    
        int pageNo = new Integer(request.getParameter("pageNo")).intValue();    
            
        try {    
            resultXML.append("<?xml version='1.0' encoding='gb2312'?>");    
            resultXML.append("<ajax-response>\n");    
            resultXML.append("<root>\n");    
   
            Pagination page = new Pagination(request, 100);    
            List list = this.getTestList(page.getStart(), page.getPageSize());    
            for (int i = 0; i < list.size(); i++) {    
                TestBean bean = (TestBean) list.get(i);    
                resultXML.append("<data>\n");    
                resultXML    
                        .append("\t<id>" + bean.getId().toString() + "</id>\n");    
                resultXML.append("\t<name>" + bean.getName() + "</name>\n");    
                resultXML.append("\t<sex>" + bean.getSex() + "</sex>\n");    
                resultXML