<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<div id=PageSelectorBar>
<div id=PageSelectorMemo>
页次:${currentPage }/${pageCount }页
每页显示:${pageSize }条
总记录数:${recordCount }
</div>
<div id=PageSelectorSelectorArea>
<a href="#" onclick="gotoPageNum(1)" title="首页" style="cursor: hand;">
<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png"/>
</a>
<s:iterator begin="beginPageIndex" end="endPageIndex" var="s">
<!-- 如果当前页是起始页,那么选中 -->
<s:if test="currentPage == #s">
<span class="PageSelectorNum PageSelectorSelected"><s:property value="#s"/></span>
</s:if>
<s:else>
<span class="PageSelectorNum" style="cursor: pointer;" onClick='gotoPageNum(<s:property value="#s"/>);'>
<s:property value="#s"/>
</span>
</s:else>
</s:iterator>
<a href="#" onclick="gotoPageNum(${pageCount})" title="尾页" style="cursor: hand;">
<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png"/></a>
转到:
<select id="pages" onchange="gotoPageNum(this.value)">
<s:iterator begin="1" end="pageCount" var="s">
<option value='<s:property value="#s"/>'>
<s:property value="#s"/>
</option>
</s:iterator>
</select>
<script type="text/javascript">
$("#pages").val(${currentPage});
</script>
</div>
</div>
<script type="text/javascript">
function gotoPageNum(pageNum){
//动态添加一个隐藏输入框,传递页码
$("#pageForm").append('<input type="hidden" value="'+pageNum+'" name="currentPage"/>');
$("#pageForm").submit();
}
</script>
以上是pageView.jsp页面的分页的列表
通过下面的代码在要使用下面页面的jsp页面中引入上面的分页列表,并提供一个pageForm表单
<%@include file="WEB-INF/jsp/public/pageView.jsp"%>
<!-- 提供一个分页用的表单 -->
<s:form id="pageForm" action="forumManage_list" namespace="/">
</s:form>
以上是分页展示的jsp页面
接下来是Pagebean对象
/**
* 封装分页信息
* @author zhaoqx
*
*/
public class PageBean {
/**从页面提交过来的参数**/
private int currentPage;//----当前页码
private int pageSize;//-------每页显示多少条数据
/**查询数据库获得**/
private int recordCount;//----总记录数
private List recordList;//页面要显示的数据集合
/**由上面4个计算获得**/
private int pageCount;//------总页数
private int beginPageIndex;//-开始页码
private int endPageIndex;//---结束页码
public PageBean() {}
public PageBean(int currentPage, int pageSize, int recordCount,List recordList) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.recordCount = recordCount;
this.recordList = recordList;
pageCount = (this.recordCount + this.pageSize - 1) / this.pageSize;//计算页数
if(pageCount <= 10){
this.beginPageIndex = 1;
this.endPageIndex = this.pageCount;
}else{
this.beginPageIndex = this.currentPage - 4;
this.endPageIndex = this.currentPage + 5;
if(this.beginPageIndex < 1){
this.beginPageIndex = 1;
this.endPageIndex = 10;
}
if(this.endPageIndex > this.pageCount){
this.endPageIndex = this.pageCount;
this.beginPageIndex = this.endPageIndex - 9;
}
}
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public int getBeginPageIndex() {
return beginPageIndex;
}
public void setBeginPageIndex(int beginPageIndex) {
this.beginPageIndex = beginPageIndex;
}
public int getEndPageIndex() {
return endPageIndex;
}
public void setEndPageIndex(int endPageIndex) {
this.endPageIndex = endPageIndex;
}
public List getRecordList() {
return recordList;
}
public void setRecordList(List recordList) {
this.recordList = recordList;
}
}
公共的分页查询工具类
package cn.itcast.oa.utils;
import java.util.ArrayList;
import java.util.List;
/**
* 辅助生成HQL语句的工具类
* @author zhaoqx
*
*/
public class HQLHelper {
private String fromStr;//FROM 子句
private String whereStr = "";//WHERE 子句
private String orderByStr = "";//ORDER BY 子句
private List<Object> args = new ArrayList<Object>();//封装HQL中对应的参数信息
public HQLHelper() {}
/**
* 通过构造方法拼接FROM 子句
* @param clazz
*/
public HQLHelper(Class clazz) {
this.fromStr = "FROM " + clazz.getSimpleName() + " o ";
}
/**
* 拼接WHERE 子句
* @param condition
* @param args
*/
public void addWhere(String condition,Object...args){//o.name = ?
if(this.whereStr.length()==0){
//第一次拼接WHERE子句
this.whereStr = " WHERE " + condition;
}else{
//不是第一次拼接WHERE子句
this.whereStr += " AND " + condition;
}
if(args != null && args.length > 0){
//封装参数
for(Object o : args){
this.args.add(o);
}
}
}
/**
* 拼接ORDER BY 子句
* @param orderBy
* @param asc
*/
public void addOrderBy(String orderBy , boolean asc){
if(this.orderByStr.length() == 0){
//第一次拼接ORDER BY 子句
this.orderByStr = " ORDER BY " + orderBy + (asc ? " ASC " : " DESC ");
}else{
//不是第一次拼接ORDER BY 子句
this.orderByStr += ", " + orderBy + (asc ? " ASC " : " DESC ");
}
}
/**
* 获取查询List集合的HQL语句
* @return
*/
public String getListHQL(){
return this.fromStr + this.whereStr + this.orderByStr;
}
/**
* 获取查询统计记录数的HQL
* @param args
*/
public String getCountHQL(){
return "SELECT COUNT(*) " + this.fromStr + this.whereStr;
}
public void setArgs(List<Object> args) {
this.args = args;
}
public List<Object> getArgs() {
return args;
}
}
接下来是流程
action中的方法
public String show(){
//根据id查询主题
Topic topic = topicService.getById(model.getId());
getValueStack().push(topic);
//根据主题查询对应的回复列表
//List<Reply> replyList = replyService.getReplyByTopic(model);
//getValueStack().set("replyList", replyList);
//PageBean pb = replyService.getPageBean(currentPage,model);
HQLHelper hh = new HQLHelper(Reply.class);
HQLHelper ha =new HQLHelper(Reply.class);
hh.addWhere("o.topic = ?", model);
hh.addOrderBy("o.postTime", true);
PageBean pb = replyService.getPageBean(hh,currentPage);
getValueStack().push(pb);
return "show";
}
底层dao的实现类
/**
*获取页面大小
*方法覆盖
*/
public PageBean getPageBean(HQLHelper hh, int currentPage) {
// 获取到页面大小
int pagesize = getPageSize();
int startFist = (currentPage - 1) * pagesize;
// 获取拼接号的Hql语句
String listHQL = hh.getListHQL();
String countHQL = hh.getCountHQL();
List<Object> args = hh.getArgs();
Session session = this.getSession();
// 获取list的HQL语句
Query query = session.createQuery(listHQL);
// 拼接查询参数
for (int i = 0; i < args.size(); i++) {
query.setParameter(i, args.get(i));
}
query.setFirstResult(startFist);
query.setMaxResults(pagesize);
List list = query.list();
Query q = session.createQuery(countHQL);
int uniqueResult = (int) q.uniqueResult();
return new PageBean(currentPage, pagesize, uniqueResult, list);
}
具体实现描述
* 分页实现方式
1)一次查询所有数据,在页面上进行显示或者隐藏
2)用多少,查多少
3) 一次查询装填在List中,通过当前页去取分页
1 写出分页页面
2 构建pageBean对象
3 hibernate或是JDBC按照分页去取数据