后台用习惯了框架分页,到做网页的时候就不自己去写分页了,并且需要的分页也是比较简单的,然后就在网上找了个简单的分页插件:kkpager
需要导入的js 和cs:
kkpager.js
kkpager.min.js
kkpager.css
效果图如下:
以下为实例:
----------BaseDao.java
//分页查询
public List<T> listByPage(String hql,int start,int maxNum,Object ...params){
Query q = getSession().createQuery(hql);
q.setFirstResult(start);
q.setMaxResults(maxNum);
for(int i = 0;i < params.length;i++){
q.setParameter(i, params[i]);
}
return q.list();
}
------------serviceImpl
public Map<String, Object> getAll(int start, int limit,StuInfo entity) {
Map<String, Object> map = HQLUtils.getHqlByEntity(entity);
List<Object> params = (List<Object>) map.get("params");
List<StuInfo> list = stuInfoDao.listByPage(map.get("queryHql") + "",start, limit,params.toArray());
long total = stuInfoDao.getCount(map.get("countHql") + "",params.toArray());
map.clear();
map.put("items", list);
map.put("total", total);
return map;
}
--------------controllelr
@Controller
@RequestMapping("/stuInfo")
public class StuInfoController{
@RequestMapping("/testPager")
public String getAll(int start,int limit,@ModelAttribute("stuInfo") StuInfo stuInfo, ModelMap map){
Map<String, Object> all = stuInfoService.getAll(start, limit, stuInfo);
map.put("total", all.get("total"));//总数量
map.put("list", all.get("items"));//分页数据
map.put("limit", limit);
return "test/testPage";
}
}
----------jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css" href="./css/kkpager.css">
<script type="text/javascript" src="./js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="./js/kkpager.js"></script>
<script type="text/javascript" src="./js/kkpager.min.js"></script>
</head>
<body>
<table id="ta">
<tr>
<td>id</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<c:forEach items="${list }" var="stu">
<tr>
<td>${stu.id }</
<td>
<td>${stu.name }</
<td>
<td>${stu.age} </
<td>
<tr>
</c:forEach>
</table>
<div id="kkpager"></div>
<input type="hidden" value="${total }" id="total">
<input type="hidden" value="${limit }" id="limit">
</body>
<script type="text/javascript">
//js获取地址栏参数的值, name为参数名
function getParameter(name) {
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
var totalPage = Math.ceil($("#total").val()/$("#limit").val());//总页数
var limit = $("#limit").val();//每页数量
var totalRecords = $("#total").val();//总数量
var pageNo = getParameter('pno');
//init
$(function(){
if(!pageNo){
pageNo = 1;
}
//生成分页
//有些参数是可选的,比如lang,若不传有默认值
kkpager.generPageHtml({
pno : pageNo,
//总页码
total : totalPage,
//总数据条数
totalRecords : totalRecords,
//链接前部
hrefFormer : 'pager_test',
//链接尾部
hrefLatter : '.html',
getLink : function(n){
var ss = (parseInt(n)-1)*parseInt(limit);
return "<%=basePath%>stuInfo/testPager?start=" + ss + "&limit="
+ limit + "&pno=" + n;
}
});
});
</script>
</html>