1. 核心类:
Pager类:
2. 需要使用分页的Action类继承该类
3.页面添加如下JSP内容即可
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.nazca.consult.consultee.web.util.Pager;
import com.opensymphony.xwork2.ActionSupport;
/**
* 分页使用到的Action,如果其他有分页需要的action继承此Action即可使用分页功能
*
* @author XiaoQuan
*/
public class AbstractListActioin extends ActionSupport {
private static final long serialVersionUID = -4680224737445304053L;
private static Log log = LogFactory.getLog(AbstractListActioin.class);
public Pager makePager(String pageNumber, int total, int pageSize,
int ListStep) {
Pager pager = new Pager();
// 设置总数
pager.setTotal(total);
// 设置每页显示多少数据
pager.setPageSize(pageSize);
int pageNum = 1;
if (pageNumber != null) {
pageNum = Integer.parseInt(pageNumber);
}
// 设置当前页
pager.setCurrentPage(pageNum);
// 最多显示分页页数
pager.setListStep(ListStep);
// 求总页数,ceil(num)取整不小于num
pager.setPagesCount((int) Math.ceil((double) pager.getTotal()
/ pager.getPageSize()));
if (pager.getPagesCount() < pageNum) {
pager.setCurrentPage(pager.getPagesCount());// 如果分页变量大总页数,则将分页变量设计为总页数
}
if (pageNum < 1) {
pager.setCurrentPage(1);
}
// 从第几页开始显示分页信息
pager.setListBegin(pager.getCurrentPage() - (int) Math.ceil((double) pager.getListStep() / 2));
if (pager.getListBegin() < 1) {
pager.setListBegin(1);
}
// 分页信息显示到第几页
pager.setListEnd(pager.getCurrentPage() + pager.getListStep() / 2);
if (pager.getListEnd() > pager.getPagesCount()) {
pager.setListEnd(pager.getPagesCount());
}
List<String> list = new ArrayList<String>();
for (int i = pager.getListBegin(); i < pager.getListEnd(); i++) {
list.add(i + "");
}
pager.setPageList(list);
return pager;
}
}
Pager类:
import java.util.List;
/**
*
* @author XiaoQuan
*/
@SuppressWarnings("unchecked")
public class Pager {
private int beginPage;
private int endPage;
private int currentPage;
private int pageSize;
private int total;
private int listStep;
private int listBegin;
private int listEnd;
private int pagesCount;
private List pageList;
public int getBeginPage() {
return beginPage;
}
public void setBeginPage(int beginPage) {
this.beginPage = beginPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getListStep() {
return listStep;
}
public void setListStep(int listStep) {
this.listStep = listStep;
}
public int getListBegin() {
return listBegin;
}
public void setListBegin(int listBegin) {
this.listBegin = listBegin;
}
public int getListEnd() {
return listEnd;
}
public void setListEnd(int listEnd) {
this.listEnd = listEnd;
}
public int getPagesCount() {
return pagesCount;
}
public void setPagesCount(int pagesCount) {
this.pagesCount = pagesCount;
}
public List getPageList() {
return pageList;
}
public void setPageList(List pageList) {
this.pageList = pageList;
}
}
2. 需要使用分页的Action类继承该类
public class GetMyQListAction extends AbstractListActioin {
中间内容略....
//获得page对象,保存到session中
Pager pageP = makePager(getPage(), total,
Constants.MY_QUESTION_PAGE_N, Constants.LIST_STEP);
ctx.put("pager", pageP);
}
3.页面添加如下JSP内容即可
<div id="pageNavigator">
<span> <s:if test="#request.pager.currentPage > 1">
<s:url id="prePage" action="consult" includeParams="none">
<s:param name="pages" value="#request.pager.currentPage-1" />
</s:url>
<s:a href="#"
onclick="javascript:GetFavoriteQList(0,%{#request.pager.currentPage-1})">上一页</s:a>
</s:if> <s:else>
<span>上一页</span>
</s:else> </span>
<span> <s:iterator value="#request.pager.pageList" id="pageList"
status="pageNum">
<s:if test="#request.pager.currentPage == #pageList">
<b>[<s:property value="pageList" />]</b>
</s:if>
<s:else>
<s:a href="#"
onclick="javascript:GetFavoriteQList(0,%{pageList})">[<s:property
value="pageList" />]</s:a>
</s:else>
</s:iterator> </span>
<span> <s:if
test="#request.pager.currentPage != #request.pager.pagesCount">
<s:url id="nextPage" action="some" includeParams="none">
<s:param name="pages" value="#request.pager.currentPage+1" />
</s:url>
<s:a href="#"
onclick="javascript:GetFavoriteQList(0,%{#request.pager.currentPage+1})">下一页</s:a>
</s:if> <s:else>
<span>下一页</span>
</s:else> </span>
</div>