分頁

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="#"
onclick="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;" onclick="adminAdd()" /></td>
<td class="page" width="80%"><s:set name="page"
value="#request.page" /> ${page.getPageDisplay()}
</td>
</tr>
</table>


</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章