jsp页面代码
<div>
<nav aria-label="Page navigation">
<ul class="pagination">
<c:if test="${pb.currentPage == 1}">
<li class="disabled">
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage - 1}&rows=5"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pb.currentPage != 1}">
<li class="active">
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage - 1}&rows=5"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<%--将域中的begin与end传入即可--%>
<c:forEach begin="${pb.begin}" end="${pb.end}" var="i">
<c:if test="${i == pb.currentPage}">
<li class="active">
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${i}&rows=5">${i}</a>
</li>
</c:if>
<c:if test="${i != pb.currentPage}">
<li>
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${i}&rows=5">${i}</a>
</li>
</c:if>
</c:forEach>
<c:if test="${pb.currentPage == pb.totalPage}">
<li class="disabled">
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage + 1}&rows=5"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pb.currentPage != pb.totalPage}">
<li class="active">
<a href="${pageContext.request.contextPath}/findPageUserServlet?currentPage=${pb.currentPage + 1}&rows=5"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<span style="font-size: 25px;margin-left: 5px;">
共${pb.totalCount}条记录,共${pb.totalPage}页
</span>
</ul>
</nav>
</div>
服务器代码实现
web层
@WebServlet("/findPageUserServlet")
public class FindPageUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String currentPage = request.getParameter("currentPage");
String rows = request.getParameter("rows");
if (currentPage == null || currentPage.trim().length() == 0){
currentPage = "1";
}
if (rows == null || rows.trim().length() == 0){
rows = "5";
}
Map<String, String[]> map = request.getParameterMap();
UserService service = new UserServiceImpl();
PageBean<User> pb = service.findPageBean(currentPage,rows,map);
request.setAttribute("pb",pb);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
service层
public class UserServiceImpl implements UserService {
private UserDao dao = new UserDaoImpl();
@Override
public PageBean<User> findPageBean(String _currentPage, String _rows, Map<String, String[]> map) {
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
if (currentPage <= 0 ){
currentPage = 1;
}
pb.setCurrentPage(currentPage);
pb.setRows(rows);
int totalCount = dao.totalCount(map);
pb.setTotalCount(totalCount);
int totalPage = totalCount % rows == 0 ? totalCount/rows : totalCount/rows+1;
pb.setTotalPage(totalPage);
int begin;
int end;
if (totalPage < 10){
begin = 1;
end = totalPage;
} else {
begin = currentPage - 5;
end = currentPage + 4;
if ( begin < 1 ){
begin = 1;
end = begin + 9;
}
if (end > totalPage){
end = totalPage;
begin = end - 9 ;
}
}
pb.setBegin(begin);
pb.setEnd(end);
return pb;
}