JSON

spring mvc实现ajax 分页

字号+ 作者:H5之家 来源:H5之家 2015-11-13 15:41 我要评论( )

文章标题:spring mvc实现ajax 分页。中国IT实验室JAVA频道是一个专业的JAVA技术平台,着眼于业界尖端技术,提供及时全面的JAVA技术和资讯文章,为广大的JAVA爱

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  使用到的技术:
  
  ·spring 3 mvc
  
  ·json
  
  ·jquery
  
  ·java
  
  ·mysql
  
  首先,要了解如何在spring mvc中使用json.
  
  以下主要从Dao和View及Controller来分析:
  
  Dao层:
  
  public List<Position> getPagePosition(int pageNum,int pageSize){
  
  //select * from t_position order by id desc limit (pageNum-1)*pagesize , pagesize
  
  List<Position> list = jdbc.query(“select * from position order by id desc limit ?,? ”, new Object[]{(pageNum-1)*pageSize>=0?(pageNum-1)*pageSize:0,pageSize},
  
  new RowMapper<Position>() {
  
  public Position mapRow(ResultSet rs, int rowNum)
  
  throws SQLException {
  
  return populate(rs);
  
  }
  
  }
  
  );
  
  return list;
  
  }
  
  Controller中代码:
  
  @RequestMapping(“/positionlist”)
  
  public String listPosition(Model model,
  
  @RequestParam(value = “pagesize”, required = false, defaultValue = “5”) int pagesize,
  
  @RequestParam(value = “pagenum”, required = false, defaultValue = “1”) int pagenum){
  
  int recordCount = positionMgr.getAll()。size();
  
  int pageCount = (recordCount+pagesize-1)/pagesize ;
  
  model.addAttribute(“pageTitle”, “Position List”);
  
  return “positionlist”;
  
  }
  
  @RequestMapping(“/positionlistajax”)
  
  public @ResponseBody List<Position> listPositionDo(Model model,
  
  @RequestParam(value = “pagesize”, required = false, defaultValue = “5”) int pagesize,
  
  @RequestParam(value = “pagenum”, required = false, defaultValue = “1”) int pagenum){
  
  List<Position> ret = positionMgr.getPagePosition(pagenum, pagesize);
  
  return ret;
  
  }
  
  View层:
  
  <script type=“text/javascript”>
  
  var pageIndex = 0;
  
  var pageSize = 5;
  
  $(function () {
  
  pageIndex = 1;
  
  AjaxGetData(pageIndex, pageSize);
  
  });
  
  function AjaxGetData( index, size) {
  
  $.ajax({
  
  url: “${pageContext.request.contextPath}/positionlistajax”,
  
  type: “Get”,
  
  data: “pagenum=” + index + “&pagesize=” + size,
  
  dataType: “json”,
  
  success: function (json) {
  
  var html = “”;
  
  html += “<table>”;
  
  html += “<thead>”;
  
  html += “<tr><th colspan=7 >Position List</th></tr>”;
  
  html += “<tr><th>ID</th><th>Name</th><th>Location</th><th>Nature</th><th>Number</th><th>End Date</th><th>Operation</th></tr>”;
  
  html += “</thead>”;
  
  html += “<tbody>”;
  
  for(position in json){
  
  html += “<tr>”;
  
  html += “<td>”+json[position].id+“</td>”;
  
  html += “<td>”+json[position].name+“</td>”;
  
  html += “<td>”+json[position].location+“</td>”;
  
  html += “<td>”+json[position].nature+“</td>”;
  
  html += “<td>”+json[position].number+“</td>”;
  
  html += “<td>”+json[position].endDate+“</td>”;
  
  html += “<td><a href='editposition?id=”+json[position].id+“‘>Edit&nbsp;<a href='position?id=”+json[position].id+“'>View</td>”;
  
  html += “</tr>”;
  
  }
  
  html += “</tbody>”;
  
  html += “<tfoot>”;
  
  html += “<tr>”;
  
  html += “<td colspan='7'>”;
  
  html += “<span>Total Records:” + ${recordCount} + “; Total Page:<span>” +${pageCount} + “” + “”;
  
  html += “<a href='javascript:void' >First&nbsp;&nbsp; ”;
  
  html += “<a href='javascript:void' >Pre&nbsp;&nbsp; ”;
  
  html += “<a href='javascript:void'>Next&nbsp;&nbsp; ”;
  
  html += “<a href='javascript:void' >Last&nbsp;&nbsp; ”;
  
  html += “<input type='text' size='4' /><input type='button' value='Jump' /> ”;
  
  html += “</td>”;
  
  html += “</tr>”;
  
  html += “</tfoot>”;
  
  html += “</table>”;
  
  //alert(html);
  
  $('#divResult’)。html(“”);
  
  $(‘#divResult’)。html(html);
  
  },
  
  error: function (XMLHttpRequest, textStatus, errorThrown) {
  
  alert(XMLHttpRequest);
  
  alert(textStatus);
  
  alert(errorThrown);
  
  }
  
  });
  
  }
  

[1] [2] 下一页

【责编:ivy】

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JavaScript转换与解析JSON方法实例详解第1/2页

    JavaScript转换与解析JSON方法实例详解第1/2页

    2016-02-10 21:25

  • 未来编程的9大猜想:JavaScript不必亲自编写

    未来编程的9大猜想:JavaScript不必亲自编写

    2016-01-30 13:01

  • Objective-C与JavaScript交互的那些事

    Objective-C与JavaScript交互的那些事

    2016-01-30 13:00

  • 常见的java代码转换成json

    常见的java代码转换成json

    2016-01-27 17:01

网友点评