Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2534642
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-16 10:44:45

参考链接:http://www.cnblogs.com/chenping-987123/archive/2011/02/14/1954640.html

使用到的技术:
·spring 3 mvc
·json
·jquery
·java
·mysql

首先,要了解如何在spring mvc中使用json

以下主要从Dao和View及Controller来分析:

Dao层:
  1. public List<Position> getPagePosition(int pageNum,int pageSize){        
  2.   //select * from t_position order by id desc limit (pageNum-1)*pagesize , pagesize    
  3.         List<Position> list = jdbc.query("select * from position order by id desc limit ?,? ", new Object[]{(pageNum-1)*pageSize>=0?(pageNum-1)*pageSize:0,pageSize},
  4.                 new RowMapper<Position>() {
  5.                     public Position mapRow(ResultSet rs, int rowNum)
  6.                             throws SQLException {
  7.                         return populate(rs);
  8.                     }
  9.                 }
  10.         );
  11.         return list;    
  12.     }

Controller中代码:
  1. @RequestMapping("/positionlist")
  2.     public String listPosition(Model model,
  3.             @RequestParam(value = "pagesize", required = false, defaultValue = "5") int pagesize,
  4.             @RequestParam(value = "pagenum", required = false, defaultValue = "1") int pagenum){
  5.         int recordCount = positionMgr.getAll().size();
  6.         int pageCount = (recordCount+pagesize-1)/pagesize ;
  7.         model.addAttribute("pageTitle", "Position List");
  8.         return "positionlist";
  9.     }
  10.     
  11.     @RequestMapping("/positionlistajax")
  12.     public @ResponseBody List<Position> listPositionDo(Model model,
  13.             @RequestParam(value = "pagesize", required = false, defaultValue = "5") int pagesize,
  14.             @RequestParam(value = "pagenum", required = false, defaultValue = "1") int pagenum){        
  15.         List<Position> ret = positionMgr.getPagePosition(pagenum, pagesize);
  16.         return ret;
  17.     }

View层:
  1. <script type="text/javascript">
  2.         var pageIndex = 0;
  3.         var pageSize = 5;
  4.         $(function () {
  5.            
  6.                 pageIndex = 1;
  7.                 AjaxGetData(pageIndex, pageSize);
  8.           
  9.         });

  10.         function AjaxGetData( index, size) {
  11.             $.ajax({
  12.                 url: "${pageContext.request.contextPath}/positionlistajax",
  13.                 type: "Get",
  14.                 data: "pagenum=" + index + "&pagesize=" + size,
  15.                 dataType: "json",
  16.                 success: function (json) {
  17.                     
  18.                      var html = "";
  19.                      html += "";
  20.                      html += "
  21. ";
  22.                      html += "
  23. ";
  24.                      html += "
  25. ";
  26.                 
  27.                      html += "
  28. ";
  29.                      html += "
  30. ";     
  31.                    for(position in json){
  32.                    html += "
  33. ";
  34.                    html += "
  35. ";
  36.                        html += "
  37. ";
  38.                        html += "
  39. ";
  40.                        html += "
  41. ";
  42.                        html += "
  43. ";
  44.                        html += "
  45. ";
  46.                        html += "
  47. ";
  48.                        html += "
  49. ";
  50.                
  51.                    }
  52.                    html += "
  53. ";
  54.                   
  55.                   html += "
  56. ";
  57.                   html += "
  58. ";
  59.                   html += "
  60. ";
  61.                   html += "
  62. ";
  63.                   html += "
  64. ";
  65.                   html += "
  66. Position List
    IDNameLocationNatureNumberEnd DateOperation
    "+json[position].id+""+json[position].name+""+json[position].location+""+json[position].nature+""+json[position].number+""+json[position].endDate+" Edit View
    ";
  67.                   html += "Total Records:" + ${recordCount} + "; Total Page:" +${pageCount} + "" + "";
  68.                   html += "First   ";
  69.                   html += "Pre   ";
  70.                   html += "Next   ";
  71.                   html += "Last   ";
  72.                   html += " ";
  73.                   html += "
  74. "
    ;
  75.                   //alert(html);
  76.                    $('#divResult').html("");
  77.                    $('#divResult').html(html);
  78.                   
  79.                 
  80.                 },
  81.                 error: function (XMLHttpRequest, textStatus, errorThrown) {
  82.                     alert(XMLHttpRequest);
  83.                     alert(textStatus);
  84.                     alert(errorThrown);
  85.                 }
  86.             });
  87.         }
  88.         
  89.         function GoToFirstPage() {
  90.             pageIndex = 1;
  91.             AjaxGetData( pageIndex, pageSize);
  92.         }
  93.         
  94.         function GoToPrePage() {
  95.             pageIndex -= 1;
  96.             pageIndex = pageIndex >= 1 ? pageIndex : 1;
  97.             AjaxGetData( pageIndex, pageSize);
  98.         }
  99.        
  100.         function GoToNextPage() {
  101.             if (pageIndex < parseInt($("#count").text())) {
  102.                 pageIndex += 1;
  103.             }
  104.                 AjaxGetData( pageIndex, pageSize);
  105.         }
  106.        
  107.         function GoToEndPage() {
  108.             pageIndex = parseInt($("#count").text()) ;
  109.             AjaxGetData( pageIndex, pageSize);
  110.         }
  111.        
  112.         function GoToAppointPage(e) {
  113.             var page = $(e).prev().val();
  114.             if (isNaN(page)) {
  115.                 alert("Page should be a valid number");
  116.             }
  117.             else {
  118.                 var tempPageIndex = pageIndex;
  119.                 pageIndex = parseInt($(e).prev().val());
  120.                 if (pageIndex <= 0 || pageIndex > parseInt($("#count").text())) {
  121.                     pageIndex = tempPageIndex;
  122.                     alert("Please input valid page scope!");
  123.                 }
  124.                 else {
  125.                     AjaxGetData(pageIndex, pageSize);
  126.                 }
  127.             }
  128.         }
  129.     </script>

  1. <div id="divResult" ></div>







 

谢谢访问!

阅读(6860) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~