package readxls;
import java.lang.reflect.*;
import java.util.*;
public class PageControl {
private int curPage = 1;// 当前是第几页
private int maxPage = 1;// 一共有多少页
private int maxRowCount = 1;// 一共有多少行
private int rowsPerPage = 20;// 每页有多少行
private List dataList;// 装载每页的数据
private String turnPageContent = "";
private void countMaxPage() { // 根据总行数计算总页数
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
}
public static int getBeginIndex(int pageIndex,int maxRowCount,int rowsPerPage) {
int countPage = 0;
if (maxRowCount % maxRowCount == 0) {
countPage = maxRowCount / rowsPerPage;
} else {
countPage = maxRowCount / rowsPerPage + 1;
}
return countPage;
}
/**
* 多个函数使用分页,使用反射机制实现,测试完成
*
* @param template
* 实现的逻辑类
* @param SetResultMethod
* 逻辑类中获取数据列表的方法名称
* @param SetAvailableCountMethod
* 逻辑类中获取数据列表总数的方法名称
* @param pgIndex
* 当前页码
* @param whereObject
* 查询条件
* @throws Exception
*/
public void loadDate(Object template, String SetResultMethod,
Integer pgIndex, Object whereObject,Integer maxCount)
throws Exception {
try {
/*
* 多个函数使用分页,使用反射机制实现,待测试
*/
Method resultMethod = template.getClass().getMethod(
SetResultMethod,
new Class[] { List.class, Integer.class, Integer.class });
this.maxRowCount = maxCount.intValue();
countMaxPage();
curPage = pgIndex.intValue();
if (curPage < 1) {
curPage = 1;
}
if (curPage > maxPage) {
curPage = maxPage;
}
int begin = (curPage - 1) * rowsPerPage;
Object[] rinArgs = new Object[3];
rinArgs[0] = whereObject;
rinArgs[1] = new Integer(begin);
rinArgs[2] = new Integer(rowsPerPage);
dataList = (List) resultMethod.invoke(template, rinArgs);
} catch (Exception e) {
throw e;
}
}
public void setDataList(List dataList) {
this.dataList = dataList;
}
public void setDataList(List dataList, int maxRowCount, int pgIndex) {
this.dataList = dataList;
this.maxRowCount = maxRowCount;
countMaxPage();
curPage = pgIndex;
if (curPage < 1) {
curPage = 1;
}
if (curPage > maxPage) {
curPage = maxPage;
}
}
public List getDataList() {
return dataList;
}
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
public int getMaxPage() {
return this.maxPage;
}
public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
}
public int getMaxRowCount() {
return this.maxRowCount;
}
public void setMaxRowCount(Object template,String SetAvailableCountMethod,Object whereObject){
/*
* 多个函数使用分页,使用反射机制实现
*/
Object[] ainArgs = new Object[1];
ainArgs[0] = (ArrayList) whereObject;
try {
Method availableCountMethod = template.getClass().getMethod(
SetAvailableCountMethod, new Class[] {List.class });
maxRowCount =((Integer)availableCountMethod.invoke(template, ainArgs)).intValue();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getRowsPerPage() {
return this.rowsPerPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public String getTurnPageContent() {
if (getMaxPage() > 1) {
turnPageContent = "每页" + getRowsPerPage() + "行 ";
turnPageContent += "共" + getMaxRowCount() + "条记录 ";
turnPageContent += "第" + getCurPage() + "页 ";
turnPageContent += "共" + getMaxPage() + "页 ";
if (getCurPage() == 1) {
turnPageContent += "首页 上一页";
} else {
turnPageContent += "<A HREF=\"javascript:gotoPage(1)\">首页</A> ";
turnPageContent += "<A HREF=\"javascript:gotoPage("
+ (getCurPage() - 1) + ")\">上一页</A>";
}
turnPageContent += " ";
if (getCurPage() == getMaxPage()) {
turnPageContent += "下一页 尾页";
} else {
turnPageContent += "<A HREF=\"javascript:gotoPage("
+ (getCurPage() + 1) + ")\">下一页</A> ";
turnPageContent += "<A HREF=\"javascript:gotoPage("
+ getMaxPage() + ")\">尾页</A> ";
}
turnPageContent += "转到第<input type=\"text\" name=\"jumpPage\" value=\""
+ getCurPage()
+ "\" size=\"5\" οnkeyup=\"if(/[^0-9]/g.test(this.value)){this.value=this.value.substr(0,this.value.length-1)}\">页";
// turnPageContent += " <img src=\"images/go_1.gif\"
// border=\"0\" style=\"cursor:hand\" οnclick=\"Jumping()\"><input
// type=\"button\" οnclick=\"Jumping()\" value=\"跳转\"
// class=\"button\">";
turnPageContent += " <img src=\"images/go_1.gif\" border=\"0\" style=\"cursor:hand\" οnclick=\"Jumping()\">";
} else {
turnPageContent = "共" + getMaxRowCount() + "条记录 ";
}
return this.turnPageContent;
}
}
2.使用分页方法的类
public class useMethod(){
public String signFeedbackList() throws Exception{
String pageIndex="1";
if(request.getParameter("pageIndex")!=null
&& ! request.getParameter("pageIndex").trim.equal("") ){
pageIndex=request.getParameter("pageIndex").trim();
}
PageControl pgControl=new PageControl();
PubInfoDaoImp imp=new PubInfoDaoImp ();
List whereList=new ArrayList();
String info=request.getParameter("info");
//加条件
whereList.add("info_id='"+info+"'");
pgControl.loadDate(imp,"","",new Integer(pageIndex),whereList);
List dataList=pgControl.getDataList();
request.setAttribute("pgControl",pgControl);
}
}
3.PubInfoDaoImp 类
public class PubInfoDaoImp extends FoundDaoTemplate implements PubInfoDao {
private static Logger logger = Logger.getLogger(PubInfoDaoImp.class);
// 查看信息根据分页
public List getSignInfoByCondition(List whereList, Integer pageIndex,
Integer rowsPerPage) {
String sql = "select * from f_zd_pubinfo where 1=1";
List list = new ArrayList();
if (whereList != null) {
for (int i = 0; i < whereList.size(); i++) {
sql = sql + " and " + (String) whereList.get(i);
}
}
sql = sql + " order by PI_COMMUNITY,INFO_ID desc";
sql = "select * from ( select a.*, rownum rnum from (" + sql
+ ") a where rownum <= ? ) where rnum > ?";
FounderJdbcTemplate template = this.getJdbcTemplate();
list = template.queryForList(sql, new Object[] {
new Integer(pageIndex.intValue() + rowsPerPage.intValue()),
pageIndex });
return list;
}
// 查看总数
public int getMaxCount(List whereList) throws Exception {
String sql_count = "select count(*) from f_zd_pubinfo where 1=1";
if (whereList != null) {
for (int i = 0; i < whereList.size(); i++) {
sql_count = sql_count + " and " + (String) whereList.get(i);
}
}
FounderJdbcTemplate template = this.getJdbcTemplate();
int maxCount = 0;
maxCount = template.queryForInt(sql_count);
return maxCount;
}
4.在页面中
signList.jsp
<%
PageControl pgControl=(PageControl)request.getAttribute("pgControl");
%>
加:<script type="text/javascript">
function gotoPage(pageIndex){
document.form1.action="xxx.action?pageIndex="+pageIndex;
document.form1.submit();
}
function Jumping(){
var pageIndex=document.form1.jumpPage.value;
gotoPage(pageIndex);
return;
}
</script>
在<body>中
<td><%=pgControl.getTurnPageContent()%></td>
使用反射机制实现分页
最新推荐文章于 2021-06-04 11:58:38 发布