Struts2 分页


写一个分页类,和大家一起分享

提供两种分页方式一种是首页,上一页,下一页,尾页,另一种是数字方式的

package com.demo.util;


import javax.servlet.http.HttpServletRequest;

/***
* 分页操作
*
* @author yaa
*
*/
public class Pagnation {
// 记录总数
private int totalRecordSize = 0;
// 页码总数
private int totalPageSize = 0;
// 当前页码
private int currentPage = 1;
// 每页要显示的记录条数
private int pageSize = 20;
// 要操作的页面的URL地址
private String pageUrl = "";
// 自定义要传递的参数字符串
private String queryString = "";
//前面及后面要显示的页码数
private int spaceSize = 5;

public int getSpaceSize() {
return spaceSize;
}

public void setSpaceSize(int spaceSize) {
this.spaceSize = spaceSize;
}

public int getTotalRecordSize() {
return totalRecordSize;
}

public void setTotalRecordSize(int totalRecordSize) {
this.totalRecordSize = totalRecordSize;
}

public int getTotalPageSize() {
return totalPageSize;
}

public void setTotalPageSize(int totalPageSize) {
this.totalPageSize = totalPageSize;
}

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 String getPageUrl() {
return pageUrl;
}

public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}

public String getQueryString() {
return queryString;
}

public void setQueryString(String queryString) {
this.queryString = queryString;
}

/**
* 构造方法
*
* @param totalRecordSize
* 记录总数量
* @param request
* HttpServletRequest
*/
public Pagnation(int totalRecordSize, HttpServletRequest request) {
this.totalRecordSize = totalRecordSize;
if (request.getParameter("page") != null) {
try {
this.currentPage = Integer.parseInt(request
.getParameter("page").toString());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* 构造方法
*
* @param totalRecordSize
* 记录总数量
* @param request
* HttpServletRequest
* @param pageUrl
* 页面操作的URL地址
*/
public Pagnation(int totalRecordSize, HttpServletRequest request,
String pageUrl) {
this.totalRecordSize = totalRecordSize;
if (request.getParameter("page") != null) {
try {
this.currentPage = Integer.parseInt(request
.getParameter("page").toString());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.pageUrl = pageUrl;
}

/***
* 获取分页生成的字符串
*
* @return
*/
public String getPagnationString() {
this.totalPageSize = (int) Math.ceil(this.totalRecordSize
/ Math.floor(this.pageSize));
if (this.currentPage < 1) {
this.currentPage = 1;
}
if (this.currentPage > this.totalPageSize && this.totalPageSize != 0) {
this.currentPage = this.totalPageSize;
}
// 分页信息生成
String componentStr = componentRender();
// 分页生成
return pageRender(componentStr);

}

/**
* 分页信息生成
*
* @param sb
*/
private String pageRender(String componentStr) {
StringBuffer sb = new StringBuffer();
sb
.append(
"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">")
.append(
"<tr><td width='20'></td><td class='zi17' align=\"right\">")
.append(componentStr).append("</td>").append(
"<td class='zi17' align=\"left\">").append("共有 ")
.append(this.totalRecordSize).append(" 条记录").append(" 共 ")
.append(this.totalPageSize).append(" 页").append(" 当前第 ")
.append(this.currentPage).append(" 页")
.append(" <input type='text' id='pagnationNum' value='1' style='width:12px;height:12px' />").append(" <input type='button' style='width:20px;height:20px' value='go' οnclick='goPage()'/>")
.append("<td></td> </tr>").append("</table>")

.append("<script type='text/javascript'>")
.append("function goPage()")
.append("{ tmpValue =document.getElementById('pagnationNum').value;")
.append(" if(isNaN(tmpValue)){ document.getElementById('pagnationNum').value='1';return;}")
.append(" window.location.href='"+this.pageUrl+"?page='+tmpValue;")
.append("}")
.append("</script>");


return sb.toString();
}

/**
* 分页生成
*
* @param sb
*/
private String componentRender() {
StringBuffer sb = new StringBuffer();

// 如果当前页码大于1
if (this.currentPage > 1) {

sb.append("[<a href='" + this.pageUrl + "?page=1");
sb.append(this.queryString);
sb.append("' class=\"\">");
sb.append("首页");
sb.append("</a>] [<a href='" + this.pageUrl + "?page=");
sb.append(currentPage - 1);
sb.append(this.queryString);
sb.append("' class=\"\">");
sb.append("前一页");
sb.append("</a>] ");
} else {// 当前页码小于1
sb.append("[首页] [前一页] ");
}
// 如果当前页码小于总页码
if (this.currentPage < this.totalPageSize) {
sb.append("<a href='" + this.pageUrl + "?page=");
sb.append(currentPage + 1);
sb.append(this.queryString);
sb.append("' class=\"\">");
sb.append("[下一页]");
sb.append("</a> [<a href='" + this.pageUrl + "?page=");
sb.append(this.totalPageSize);
sb.append(this.queryString);
sb.append("' class=\"\">");
sb.append("尾页");
sb.append("</a>] ");
} else {// 当前页码大于总页码
sb.append("[下一页] [尾页] ");
}
return sb.toString();
}

/// <summary>
/// 设置分页内容
/// </summary>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">每页显示信息条数</param>
/// <param name="pageSize">当前页面URL地址</param>
/// <returns>分页内容对象</returns>
public String getNumPageString()
{
this.totalPageSize = (int) Math.ceil(this.totalRecordSize
/ Math.floor(this.pageSize));
if (this.currentPage < 1) {
this.currentPage = 1;
}
if (this.currentPage > this.totalPageSize && this.totalPageSize != 0) {
this.currentPage = this.totalPageSize;
}

String pageFirstStr = "页码:";

int preIndex = 0;
int endIndex = 0;
preIndex = (currentPage - spaceSize) < 1 ? 1 : currentPage - spaceSize;
endIndex = (currentPage + spaceSize < spaceSize * 2 ? spaceSize * 2 : (currentPage + spaceSize)) > totalPageSize ? totalPageSize : currentPage + spaceSize;

for (int i = preIndex - 1; i < endIndex; i++)
{
if ((i + 1) == currentPage)
{
pageFirstStr += "&nbsp;<b><font color='#ff0000'>" + currentPage + "</font></b>";
}
else
{
pageFirstStr += "&nbsp;<a href='" + this.pageUrl + "?page=" + (i + 1) + "" + this.queryString + "'>" + (i + 1) + "</a>";
}
}

int toNum = 0;

if (currentPage == totalPageSize)
{
toNum = totalRecordSize % pageSize == 0 ? pageSize : totalRecordSize % pageSize;
}
else
{
toNum = pageSize;
}
pageFirstStr += "&nbsp;共有" + totalRecordSize + "条记录,每页" + pageSize + "条,共有" + totalPageSize + "页,当前第" + currentPage + "页,第" + ((currentPage - 1) * pageSize + 1) + "条至第" + ((currentPage - 1) * pageSize + toNum) + "条。";
String pageSecondStr = "&nbsp;跳转到:<input type='text' id='page' class='input' οnblur='chkNum(this)' size='3' value='1'>&nbsp;页&nbsp;<input class='btn' type=button value=跳转 οnclick=\"window.location.href='" + this.pageUrl + "?page='+document.getElementById('page').value+'" + this.queryString + "';\"><input type='hidden' id='totalPageSize' value='" + totalPageSize + "'>";



StringBuilder sbTable = new StringBuilder();
sbTable.append("<table id=\"pagation\" width=\"100%\"");
sbTable.append("<tr id=\"pa\">");
sbTable.append(pageFirstStr);
sbTable.append("<td align=\"left\" height=\"30px\">");
sbTable.append("</td>");
sbTable.append("<td align=\"right\" height=\"30px\">");
sbTable.append(pageSecondStr);
sbTable.append("</td>");
sbTable.append("</tr>");
sbTable.append("</table>");

return sbTable.toString();
}
}
使用方法

在action对应的方法中加入

// 分页操作
Pagnation pagnation = new Pagnation(totalCount, request, url);
totalCount //数据总数

request

url //要进行分页的地址

还有其它的一些属性可能使用,已经在分页类里面有说明

pagnation.getPagnationString()//获取分页生成的字符串

要action类中定义一个pageStr的字段串设置set,get方法,把生成的字符串装配进去

在jsp页面 可以使用struts2 的标签 <s:property value="pageStr" escape="false" />即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值