分页

package com.util;


import java.util.List;


//封装页面显示逻辑
public class Page
{


private int totle;// 总共的数据量


private int pageSize;// 每页显示多少条


private int totlePage;// 共有多少页


private int index;// 当前是第几页


private List data; // 数据


private String path;// 连接路径


public void setTotle(int totle)
{
this.totle = totle;
}


public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}


public void setIndex(int index)
{
this.index = index;
}


public void setPath(String path)
{
this.path = path;
}


public int getTotle()
{
return totle;
}


public int getPageSize()
{
return pageSize;
}


public int getTotlePage()
{
return (this.totle + this.pageSize - 1) / this.pageSize;
}


public int getIndex()
{
return index;
}


public List getData()
{
return data;
}


public void setData(List data)
{
this.data = data;
}


public String getPageDisplay()
{
StringBuffer displayInfo = new StringBuffer();
if (index == 0 || pageSize == 0)
{
displayInfo.append("没有分页的信息!");
} else
{
displayInfo.append("<div>");
displayInfo.append("共" + totle
+ "条记录&nbsp;&nbsp;&nbsp;&nbsp;每页<span style='color:#FF0000'>" + pageSize
+ "</span>条&nbsp;&nbsp;&nbsp;&nbsp;");
displayInfo.append("第<span style='color:#FF0000'>" + index
+ "</span>页/共" + this.getTotlePage() + "页&nbsp;&nbsp;&nbsp;&nbsp;");
// 判断如果当前是第一页 则“首页”和“第一页”失去链接
if (index == 1)
{
displayInfo.append("首页&nbsp;&nbsp;&nbsp;&nbsp;");
displayInfo.append("上一页&nbsp;&nbsp;&nbsp;&nbsp;");
} else
{
displayInfo.append("<a href='" + path
+ "index=1'>首页&nbsp;&nbsp;&nbsp;&nbsp;</a>");
displayInfo.append("<a href='" + path + "index=" + (index - 1)
+ "'>上一页&nbsp;&nbsp;&nbsp;&nbsp;</a>&nbsp;");
}
if (index >= this.getTotlePage())
{
displayInfo.append("下一页&nbsp;&nbsp;&nbsp;&nbsp;");
displayInfo.append("末页&nbsp;&nbsp;&nbsp;&nbsp;");
} else
{
displayInfo.append("<a href='" + path + "index=" + (index + 1)
+ "'>下一页&nbsp;&nbsp;&nbsp;&nbsp;</a>");
displayInfo.append("<a href='" + path + "index="
+ this.getTotlePage() + "'>末页</a>&nbsp;&nbsp;&nbsp;&nbsp;");
}
displayInfo.append("</div>");
}
return displayInfo.toString();
}
}
Action类
package com.action;


import java.util.List;
import java.util.Map;


import org.apache.struts2.ServletActionContext;


import com.dao.TAdminDAO;
import com.model.TAdmin;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Page;


public class adminAction extends ActionSupport
{
private int userId;
private String userName;
private String userPw;
 
private String message;
private String path;

private int index=1;


private TAdminDAO adminDAO;


public String adminAdd()
{
TAdmin admin=new TAdmin();
admin.setUserName(userName);
admin.setUserPw(userPw);
adminDAO.save(admin);
this.setMessage("操作成功");
this.setPath("adminManage.action");
return "succeed";
}



public String adminManage()
{
List adminList=adminDAO.findAll();
Map request=(Map)ServletActionContext.getContext().get("request");
int pageSize=10;
int fromIndex = (index - 1) * pageSize;
int toIndex = Math.min(fromIndex + pageSize, adminList.size());
List adminListFenye = adminList.subList(fromIndex, toIndex);



        Page p = new Page();//创建 分页对象
        p.setIndex(index);//设置页数
        p.setPageSize(pageSize);
        p.setTotle(adminList.size());//设置总共的条数
        p.setData(adminListFenye);//设置数据
        p.setPath("adminManage.action?");//跳转的路径


request.put("page", p);
request.put("adminList", adminList);
return ActionSupport.SUCCESS;
}

public String adminManageFenye()
{
List adminList=adminDAO.findAll();
int pageSize=10;
int fromIndex = (index - 1) * pageSize;
int toIndex = Math.min(fromIndex + pageSize, adminList.size());
List adminListFenye = adminList.subList(fromIndex, toIndex);



        Page p = new Page();//创建 分页对象
        p.setIndex(index);//设置页数
        p.setPageSize(pageSize);
        p.setTotle(adminList.size());//设置总共的条数
        p.setData(adminListFenye);//设置数据
        p.setPath("adminManageFenye.action?");//跳转的路径


Map request=(Map)ServletActionContext.getContext().get("request");
request.put("page", p);
return ActionSupport.SUCCESS;
}

public String adminDel()
{
adminDAO.delete(adminDAO.findById(userId));
this.setMessage("删除成功");
this.setPath("adminManage.action");
return "succeed";
}




public TAdminDAO getAdminDAO()
{
return adminDAO;
}


public void setAdminDAO(TAdminDAO adminDAO)
{
this.adminDAO = adminDAO;
}


public String getMessage()
{
return message;
}


public int getIndex()
{
return index;
}






public void setIndex(int index)
{
this.index = index;
}






public void setMessage(String message)
{
this.message = message;
}


public String getPath()
{
return path;
}


public void setPath(String path)
{
this.path = path;
}


public int getUserId()
{
return userId;
}


public void setUserId(int userId)
{
this.userId = userId;
}


public String getUserName()
{
return userName;
}


public void setUserName(String userName)
{
this.userName = userName;
}


public String getUserPw()
{
return userPw;
}


public void setUserPw(String userPw)
{
this.userPw = userPw;
}
 
}
Jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />


<link rel="stylesheet" type="text/css" href="<%=path%>/css/base.css" />


<script language="javascript">
           function adminDel(userId)
           {
               if(confirm('您确定删除吗?'))
               {
                   window.location.href="<%=path%>/adminDel.action?userId="+userId;
               }
           }
           function adminAdd()
           {
          var url="<%=path%>/admin/index/adminAdd.jsp";
              window.location.href=url;
           }
       </script>
</head>


<body leftmargin="2" topmargin="2" background='<%=path%>/img/allbg.gif'>
<table width="98%" border="0" cellpadding="2" cellspacing="1"
bgcolor="#D1DDAA" align="center" style="margin-top: 8px">
<tr bgcolor="#E7E7E7">
<td height="14" colspan="4" background="<%=path%>/img/tbg.gif">&nbsp;会员管理&nbsp;</td>
</tr>
<tr align="center" bgcolor="#FAFAF1" height="22">
<td width="20%">序号</td>
<td width="30%">账号</td>
<td width="30%">密码</td>
<td width="20%">操作</td>
</tr>
<s:if test="#request.page.data==null || #request.page.data.size() == 0">
<tr>
<td colspan="4"><font size="15" color="red">没有数据</font></td>
</tr>
</s:if>
<s:else>
<s:iterator value="#request.page.data" status="ss" id="user">
<tr align='center' bgcolor="#FFFFFF"
onMouseMove="javascript:this.bgColor='red';"
onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td bgcolor="#FFFFFF" align="center"><s:property
value="#ss.index+1" /></td>
<td bgcolor="#FFFFFF" align="center"><s:property
value="#user.userName" /></td>
<td bgcolor="#FFFFFF" align="center"><s:property
value="#user.userPw" /></td>
<td bgcolor="#FFFFFF" align="center"><a href="#"
οnclick="adminDel(<s:property value="#user.userId"/>)"
class="pn-loperator">删除</a></td>
</tr>
</s:iterator>
</s:else>
</table>
<table width='98%' border='0'
style="margin-top: 8px; margin-left: 5px;">
<tr align='center'>
<td align='left'><input type="button" value="添加管理员"
style="width: 80px;" οnclick="adminAdd()" /></td>
<td class="page" width="80%"><s:set name="page"
value="#request.page" /> ${page.getPageDisplay()}
</td>
</tr>
</table>


</body>
</html>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值