最终效果如图:
pager.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>Pager</description> <tlib-version>1.0</tlib-version> <short-name>page</short-name> <uri></uri> <tag> <name>createPager</name> <tag-class>com.test.utils.Pager</tag-class> <body-content>JSP</body-content> <attribute> <name>curPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>totalPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>pageSize</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>totalCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Integer</type> </attribute> <attribute> <name>formId</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /*** * 分页控件 * @author 花非花雾非雾 */ public class Pager extends TagSupport { private static final long serialVersionUID = 1L; private Integer curPage; private Integer totalPage; private Integer pageSize = TestConstance.pageSize; private Integer totalCount = 0; private String formId; public void setCurPage(Integer curPage) { this.curPage = curPage; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public void setFormId(String formId) { this.formId = formId; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); int pageNumber = 0; if (totalPage%pageSize==0) { pageNumber = totalPage/pageSize; } else { pageNumber = (totalPage/pageSize)+1; } if (curPage < 1) { curPage = 1; } try { if (pageNumber > 0) { out.print("<script type='text/javascript'>" + "function go(pageNum){" + "var f = document.getElementById('" + formId + "');"+ "f.action = f.action + '?pageNum=' + pageNum + '&pageSize="+pageSize+"';"+ "f.submit();"+ "}" + "</script>"); out.print("<div class='pagination'><ul>"); int start = 1; int end = totalPage; for(int i=4;i>=1;i--){ if((curPage-i)>=1){ start = curPage-i; break; } } for(int i=4;i>=1;i--){ if((curPage+i)<=totalPage){ end = curPage+i; break; } } //如果小于9则右侧补齐 if(end-start+1<=9){ Integer padLen = 9-(end-start+1); for(int i=padLen;i>=1;i--){ if((end+i)<=totalPage){ end = end+i; break; } } } //如果还小于9左侧补齐 if(end-start+1<=9){ Integer padLen = 9-(end-start+1); for(int i=padLen;i>=1;i--){ if((start-i)>=1){ start = start-i; break; } } } if(curPage>1){ if(start>1){ out.print("<li><a href='javascript:go(1)'>首页</a></li>"); } out.print("<li><a href='javascript:go("+(curPage-1)+")'>上一页</a></li>"); } for(int i=start;i<=end;i++){ if(i==curPage){ out.print("<li class='active'><a href='#'>" + i + "</a></li>"); }else{ out.print("<li><a href='javascript:go("+i+")'>" + i + "</a></li>"); } } if(curPage<totalPage){ out.print("<li><a href='javascript:go("+(curPage+1)+")'>下一页</a></li>"); if(end<totalPage){ out.print("<li><a href='javascript:go("+totalPage+")'>尾页</a></li>"); } } out.print("<li><a href='javascript:void(0)'>共" + totalPage + "页" + this.totalCount + "条</a></li>"); out.print("</ul>"); } } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } public static Integer getStartIndex(Integer pageNum, Integer pageSize){ Integer res = 0; if(pageNum>0){ res = (pageNum-1)*pageSize; } return res; } }
import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.ui.Model; public class BaseController { //初始化分页相关信息 protected void initPage(Map<String,Object> map, Integer pageNum, Integer pageSize, Integer totalCount){ if(null==pageSize || pageSize.equals("")){ pageSize = FundTestConstance.pageSize; } if(pageSize>50){ pageSize = 50; } Integer totalPage = (totalCount+pageSize-1)/pageSize; if(null==pageNum){ pageNum = 1; }else if(pageNum>totalPage){ pageNum = totalPage; } map.put("startIndex", Pager.getStartIndex(pageNum, pageSize)); map.put("pageNum", pageNum); map.put("totalPage", totalPage); map.put("pageSize", pageSize); map.put("totalCount", totalCount); } //将相关数据放入model protected void initResult(Model model, List<Object> list, Map<String,Object> map){ model.addAttribute("list", list); Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry m = (Map.Entry)it.next(); model.addAttribute(m.getKey().toString(), m.getValue()); } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="page" uri="/WEB-INF/pager.tld"%> <% String path = request.getContextPath(); %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>测试分页标签</title> <link href="<%=path%>/bootstrap/css/bootstrap.css" rel="stylesheet"> <link href="<%=path%>/bootstrap/css/bootstrap-responsive.css" rel="stylesheet"> <script type="text/javascript"> function toDel(id){ var url = "<%=path%>/test/del?id=" + id +"&pageNum=${param.pageNum}&pageSize=${param.pageSize}"; window.location.href = url; } </script> </head> <body> <div class="container"> <c:forEach items="${list}" var="item"> <div class="border-bottom1"> <h3><a href="<%=path%>/test/view?id=${item.id}">${item.name}</a></h3> <p> ${item.content} </p> <p class="text-right muted"> 2013-06-22 22:37 <a href="javascript:toDel('${item.id}');">删除</a> <a href="<%=path%>/test/toEdit?id=${item.id}&pageNum=${param.pageNum}&pageSize=${param.pageSize}">编辑</a> </p> </div> </c:forEach> <form method="post" id="testForm" action="<%=path%>/test"> <input type="hidden" name="type" value="${type}"> </form> <page:createPager pageSize="${pageSize}" totalPage="${totalPage}" totalCount="${totalCount}" curPage="${pageNum}" formId="testForm"/> </div> </body> </html>
TestController.java
@Controller public class TestController extends BaseController{ @Autowired private TestService testService; public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } @RequestMapping("/test") public String test(Model model, @RequestParam(required=false) String type, @RequestParam(required=false) Integer pageNum, @RequestParam(required=false) Integer pageSize) { Map<String,Object> map = new HashMap<String,Object>(); map.put("type", type); Integer totalCount = this.testService.getTestsCount(map); this.initPage(map, pageNum, pageSize, totalCount); List list = this.testService.getTests(map); this.initResult(model, list, map); return "test"; } @RequestMapping("/test/add") public String testAdd(@RequestParam String type,Model model) { Test test = new Test(); test.setType(type); this.testService.addTest(test); return this.test(model,null,null,null); } @RequestMapping("/test/del") public String testDel(@RequestParam(required=true) Integer id, @RequestParam(required=false) Integer pageNum, @RequestParam(required=false) Integer pageSize, Model model) { this.testService.delTest(id); return this.test(model, null, pageNum, pageSize); } @RequestMapping("/test/toEdit") public String testToEdit(@RequestParam(required=true) Integer id, Model model) { Test test = this.testService.getTestById(id); model.addAttribute("test", test); return "testEdit"; } @RequestMapping("/test/edit") public String testedit(@RequestParam(required=true) Integer id, @RequestParam String type, @RequestParam(required=false) Integer pageNum, @RequestParam(required=false) Integer pageSize, Model model) { Test test = new Test(); test.setType(type); this.testService.editTest(test); return this.test(model, null, pageNum, pageNum); } }
TestMapper.xml
<select id="getTests" resultMap="baseResultMap" parameterType="map"> SELECT id, name, type, create_date, update_date FROM test WHERE 1=1 <if test="type!=null and type!=''"> AND type = #{type} </if> limit #{startIndex},#{pageSize} </select> <select id="getTestsCount" resultType="java.lang.Integer" parameterType="map"> SELECT COUNT(1) FROM test WHERE 1=1 <if test="type!=null and type!=''"> AND type = #{type} </if> </select>