package cn.myic.model;
import java.util.List;
public class Page<E> {
// 结果集
private List<E> list;
// 查询记录总数
private int totalRecords;
// 每页多少条记录
private int pageSize;
// 第几页
private int pageNo;
/**
* @return 总页数
* */
public int getTotalPages(){
return (totalRecords+pageSize-1)/pageSize;
}
/**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public int countOffset(int currentPage,int pageSize){
int offset = pageSize*(currentPage-1);
return offset;
}
/**
* @return 首页
* */
public int getTopPageNo(){
return 1;
}
/**
* @return 上一页
* */
public int getPreviousPageNo(){
if(pageNo<=1){
return 1;
}
return pageNo-1;
}
/**
* @return 下一页
* */
public int getNextPageNo(){
if(pageNo>=getBottomPageNo()){
return getBottomPageNo();
}
return pageNo+1;
}
/**
* @return 尾页
* */
public int getBottomPageNo(){
return getTotalPages();
}
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
/**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return 返回查询记录集合
*/
@SuppressWarnings("unchecked")
@Override
public List<Course> queryForPage(int offset, int length) {
// TODO Auto-generated method stub
List<Course> entitylist=null;
try{
Query query = getSession().createQuery("from Course");
query.setFirstResult(offset);
query.setMaxResults(length);
entitylist = query.list();
}catch(RuntimeException re){
throw re;
}
return entitylist;
}
/**
* 分页查询
* @param currentPage 当前页号:现在显示的页数
* @param pageSize 每页显示的记录条数
* @return 封闭了分页信息(包括记录集list)的Bean
* */
@SuppressWarnings("unchecked")
@Override
public Page queryForPage(int currentPage,int pageSize) {
// TODO Auto-generated method stub
Page page = new Page();
//总记录数
int allRow = courseDao.getAllRowCount();
//当前页开始记录
int offset = page.countOffset(currentPage,pageSize);
//分页查询结果集
List<Course> list = courseDao.queryForPage(offset, pageSize);
page.setPageNo(currentPage);
page.setPageSize(pageSize);
page.setTotalRecords(allRow);
page.setList(list);
return page;
}
@RequestMapping(value = "/showAll.do")
public String findAllCourse(HttpServletRequest request,
HttpServletResponse response) {
try {
String pageNo = request.getParameter("pageNo");
if (pageNo == null) {
pageNo = "1";
}
Page page = courseService.queryForPage(Integer.valueOf(pageNo), 10);
request.setAttribute("page", page);
List<Course> course = page.getList();
request.setAttribute("courses", course);
} catch (Exception e) {
e.printStackTrace();
}
return "course/course_list";
}
<tr>
<td colspan="6" align="center" bgcolor="#5BA8DE">共${page.totalRecords}条记录 共${page.totalPages}页 当前第${page.pageNo}页<br>
<a href="${path}/course/showAll.do?pageNo=${page.topPageNo }"><input type="button" name="fristPage" value="首页" /></a>
<c:choose>
<c:when test="${page.pageNo!=1}">
<a href="${path}/course/showAll.do?pageNo=${page.previousPageNo }"><input type="button" name="previousPage" value="上一页" /></a>
</c:when>
<c:otherwise>
<input type="button" disabled="disabled" name="previousPage" value="上一页" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.pageNo != page.totalPages}">
<a href="${path}/course/showAll.do?pageNo=${page.nextPageNo }"><input type="button" name="nextPage" value="下一页" /></a>
</c:when>
<c:otherwise>
<input type="button" disabled="disabled" name="nextPage" value="下一页" />
</c:otherwise>
</c:choose>
<a href="${path}/course/showAll.do?pageNo=${page.bottomPageNo }"><input type="button" name="lastPage" value="尾页" /></a>
</td>
</tr>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有