1. 分页引用:
class="java" name="code"><jsp:include page="/jsp/common/resource_page.jsp"/>
2. resource_page.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<c:set var="basePath" value="${pageContext.request.contextPath }" />
<ul class="pagination">
<c:if test="${pages <=5}">
<c:forEach items="${pagesList}" var="p" varStatus="status">
<li <c:if test="${page eq status.index+1}">class="active"</c:if>><a
href="javascript:void(0)"
<c:if test="${page ne status.index+1}">onclick="searchList(${status.index+1})"</c:if>>${status.index+1}</a>
</li>
</c:forEach>
</c:if>
<c:if test="${pages >5}">
<li <c:if test="${page eq 1}">class="disabled"</c:if>><a
href="javascript:void(0)"
<c:if test="${page ne 1}">onclick="searchList(${page-1})"</c:if>>«</a>
</li>
<c:forEach items="${pagesList}" var="p" varStatus="status" begin="${beginPage }" end="${endPage }">
<li <c:if test="${page eq status.index+1}">class="active"</c:if>><a
href="javascript:void(0)"
<c:if test="${page ne status.index+1}">onclick="searchList(${status.index+1})"</c:if>>${status.index+1}</a>
</li>
</c:forEach>
<li <c:if test="${page eq pages}">class="disabled"</c:if>><a
href="javascript:void(0)"
<c:if test="${page ne pages}">onclick="searchList(${page+1})"</c:if>>»</a>
</li>
</c:if>
</ul>
3. 根据页码
请求数据:
function searchList(page){
var basePath = '${basePath}';
document.thisform.action = basePath+"/trade/queryOrder.htm?page="+page+"&rows=20";
$("#thisform").submit();
}
4.
controller编码:
@RequestMapping(value = "/queryOrder.htm")
public String queryOrderInfo(Model model, HttpServletRequest request,
HttpServletResponse response) throws ParseException {
LOGGER.info(JSON.toJSONString(request.getParameterMap()));
// 分页数据
String page = request.getParameter("page");
String size = request.getParameter("rows");
int offset = 0;
if (null!=page&&!"".equals(page))
offset = (Integer.parseInt(page) - 1) * Integer.parseInt(size);
else
page = "1";
int limit = 20;
if (null!=size&&!"".equals(size))
limit = Integer.parseInt(size);
//查询结果
OrderDetailBo order = new OrderDetailBo();
order = setOrderDetailBoValue(request, model, response, order);
if (order == null) {
return "forward:../index.htm";
}
List<OrderDetailBo> list = orderDetailService.queryPageList(order,offset,limit);
int total = orderDetailService.queryCount(order);
//返回页面model
model.addAttribute("orderList", list);
pageBuildParameters(model,total,Integer.parseInt(page),limit);
return "trade/orderQuery";
}
5. pageBuildParameters方法的编码:
private void pageBuildParameters(Model model,int total,int page,int limit) {
model.addAttribute("total", total);
model.addAttribute("page", page);
model.addAttribute("size", limit);
int pages = total%limit>0?(total/limit)+1:(total/limit);
// pages = 5;测试
model.addAttribute("pages", pages);
List<Integer> pagesList = new ArrayList<Integer>();
for (int i=0; i<pages; i++) {
pagesList.add(i+1);
}
model.addAttribute("pagesList", pagesList);
int beginPage = 0;
int endPage = 4;
if (pages>5) {
int pageGroup = page%5>0?(page/5)+1:(page/5);
if (pages-page>=5) {
beginPage = (pageGroup-1)*5;
endPage = pageGroup*5-1;
} else {
beginPage = pages-5;
endPage = pages;
}
}
model.addAttribute("beginPage", beginPage);
model.addAttribute("endPage", endPage);
}