如果数据库查询的条数太多,在一个网页不方便显示时,考虑分页,只把部分送入前台,减少了数据传输量,提高了显示速度。
1、后台方法:在servlet,controller或action中得到分页后的文章,送往前台
import org.apache.log4j.Logger;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import shown.JdbcUtil;
public class PageUtil
private static final Logger logger = Logger.getLogger(PageUtil.class);
/**
* 得到分页查询后的列表
* @param queryString 查询语句
* @param countPerPage 每页显示的记录条数
* @param pagePerShow 每页上显示的页码数
*/
public static List pagedList(HttpServletRequest request,String queryString,Integer countPerPage,Integer pagePerShow){
ApplicationContext apc=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
JdbcTemplate jdbcTemplate=(JdbcTemplate)apc.getBean("jdbcTemplate");
String currPage=request.getParameter("PAGED_DBQUERY_CURRENT");
String totalPage=request.getParameter("PAGED_DBQUERY_TOTAL");
if (currPage==null||currPage.equals("")) {
currPage="1";//初始化当前页
}if (pagePerShow==null||pagePerShow<=2) {
pagePerShow=10;//初始化显示的页码数
}if (countPerPage==null||countPerPage<=0) {
countPerPage=10;//初始化每页显示的记录条数
}if (totalPage==null||totalPage.equals("")) {
int results=jdbcTemplate.queryForInt("select count(*) from ("+queryString+") as querytable");
int t=results%countPerPage==0?results/countPerPage:results/countPerPage+1;
totalPage=results==0?"0":t+"";
//初始化总页码,如果request没有再查询,避免每次都查询数据库,减少不必要的查询浪费
}
queryString+=" limit "+(Integer.parseInt(currPage)-1)*countPerPage+","+countPerPage;
request.setAttribute("PAGED_DBQUERY_CURRENT", currPage);
request.setAttribute("PAGED_DBQUERY_TOTAL", totalPage);
request.setAttribute("PAGED_DBQUERY_PAGEPERSHOW", pagePerShow);
logger.debug(queryString);//打印:附带了分页信息的查询语句
return jdbcTemplate.queryForList(queryString);
}
2、前台页面:在需要显示内容的网页上:
<form name="PAGED_DBQUERY_FORM" method="post" action="test.do">
<jsp:include page="/pagedDbQuery.jsp?form_name=PAGED_DBQUERY_FORM"/>
</form>
3、处理页面:pagedDbQuery.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<script type="text/javascript">
function formsub(current){
var current_form=document.forms['${param.form_name}'];
current_form.action=current_form.action+'?PAGED_DBQUERY_TOTAL='+${PAGED_DBQUERY_TOTAL}+'&PAGED_DBQUERY_CURRENT='+current;
current_form.submit();
}
</script>
<div>
<%
int curpage=Integer.parseInt(request.getAttribute("PAGED_DBQUERY_CURRENT").toString()); //当前页
int total=Integer.parseInt(request.getAttribute("PAGED_DBQUERY_TOTAL").toString()); //总页码
int pagePerShow=Integer.parseInt(request.getAttribute("PAGED_DBQUERY_PAGEPERSHOW").toString())/2;//每页显示的分页数
int pre=curpage>1?curpage-1:1; //上一页
int next=curpage<total?curpage+1:total; //下一页
int begin=curpage>pagePerShow?curpage-pagePerShow:1; //分页起始页
int end=curpage+pagePerShow<total?curpage+pagePerShow:total; //分页结束页
%>
<span>共<%=total%>页</span>
<span><a href="javascript:formsub(<%=pre%>)">上一页</a></span>
<span>第</span>
<c:forEach begin="<%=begin%>" end="<%=end%>" varStatus="current">
<span><a href="javascript:formsub(<%=begin%>)"><%=begin++%></a></span>
</c:forEach>
<span>页</span>
<span><a href="javascript:formsub(<%=next%>)">下一页</a></span>
</div>
附:处理页面 pagedDbQuery.jsp 的另一种实现方式:用纯页面java代码实现
把<c:forEach>部分也换成java代码,这样页面就不用jstl了,对喜欢在jsp写脚本的弟兄们可能更舒服:
<%