效果图
未经处理的前端代码
<div style="text-align: center">
<div class="laypage-main">
<span class="laypage-curr">1</span>
<a href="/jie/page/2/">2</a>
<a href="/jie/page/3/">3</a>
<a href="/jie/page/4/">4</a>
<a href="/jie/page/5/">5</a>
<span>…</span>
<a href="/jie/page/148/" class="laypage-last" title="尾页">尾页</a>
<a href="/jie/page/2/" class="laypage-next">下一页</a>
</div>
</div>
处理后的前端代码
<div style="text-align: center">
<div class="laypage-main">
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>" class="laypage-next">上一页</a>
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=1" class="laypage-last">首页</a>
<s:if test="#pastePageBean.currentPage-3>0">
<span>…</span>
</s:if>
<s:if test="#pastePageBean.currentPage-2>0">
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-2"/>">
<s:property value="#pastePageBean.currentPage-2" />
</a>
</s:if>
<s:if test="#pastePageBean.currentPage-1>0">
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>">
<s:property value="#pastePageBean.currentPage-1" />
</a>
</s:if>
<!-- 当前页 -->
<span class="laypage-curr">
<s:property value="#pastePageBean.currentPage" />
</span>
<s:if test="#pastePageBean.currentPage+1<=#pastePageBean.totalPage">
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>">
<s:property value="#pastePageBean.currentPage+1" />
</a>
</s:if>
<s:if test="#pastePageBean.currentPage+2<=#pastePageBean.totalPage">
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+2"/>">
<s:property value="#pastePageBean.currentPage+2" />
</a>
</s:if>
<s:if test="#pastePageBean.currentPage+3<=#pastePageBean.totalPage">
<span>…</span>
</s:if>
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾页">尾页</a>
<a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一页</a>
</div>
</div>
分页实体
PageBean.class
package com.zsj.utils;
import java.util.List;
public class PageBean {
//页面大小
private Integer pageSize;
//当前页
private Integer currentPage;
//总条数
private Integer totalCount;
//总页数
private Integer totalPage;
//每页显示的数据
private List list;
public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {
this.currentPage = currentPage;
this.totalCount = totalCount;
this.pageSize = pageSize;
//安全校验 跳转到页面时,没有携带数据,此时currentPage是空的。默认第一页
if(this.currentPage == null) {
this.currentPage = 1;
}
if(this.pageSize == null) {
this.pageSize = 8;
}
//计算totalPage 向上取整 当总数据为10一页大小为5时,是2页。一页大小是3时,是4页。
this.totalPage = (int) Math.ceil(1.0 * this.totalCount/this.pageSize);
//安全校验 (防止恶意输入当前页)
if(this.currentPage > this.totalPage) {
this.currentPage = this.totalPage;
}
if(this.currentPage < 1) {
this.currentPage = 1;
}
}
//获得起始索引(select * from paste limit ?,? 即第一个?的值)
public Integer getStart() {
return (this.currentPage-1)*this.pageSize;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
Action
GetDataAction.class
package com.zsj.web;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zsj.domain.Paste;
import com.zsj.service.PasteService;
import com.zsj.utils.PageBean;
public class GetDataAction extends ActionSupport{
private PasteService pasteService;
private Integer currentPage;
public String getData() throws Exception {
PageBean pastePageBean = pasteService.getPastePageBean(currentPage);
ActionContext.getContext().put("pastePageBean", pastePageBean);
return "index";
}
public PasteService getPasteService() {
return pasteService;
}
public void setPasteService(PasteService pasteService) {
this.pasteService = pasteService;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
}
Service
//分页查询
public PageBean getPastePageBean(Integer currentPage) {
Integer totalCount = pasteDao.findAllPasteNum();
PageBean pageBean = new PageBean(currentPage, totalCount, 8);
List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize());
pageBean.setList(list);
System.out.println("currentPage = "+currentPage + " totalCount = " + totalCount);
return pageBean;
}
Dao
//查询帖子总数,获得totalCount
public Integer findAllPasteNum() {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select count(*) from paste";
NativeQuery query = session.createSQLQuery(sql);
BigInteger result = (BigInteger) query.uniqueResult();
return result.intValue();
}
//分页获得帖子的List
public List<Paste> getPastePageList(Integer start, Integer pageSize) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from paste limit ?,?";
NativeQuery query = session.createSQLQuery(sql);
//System.out.println("start = "+start);
query.addEntity(Paste.class);
query.setParameter(1, start);
query.setParameter(2, pageSize);
List list = query.list();
return list;
}