日期:2014-05-20  浏览次数:20749 次

求JSP+struts2+SQLServer 分页源代码?
请各位大侠给出完整的分页源代码,谢谢!

------解决方案--------------------
private final static int page_size = 3;
private int totalRows;// 总行数
private int pageSize = page_size;// 每页显示的行数
private int currentPage;// 当前页号
private int totalPages;// 总的页数
private boolean hasNext;// 是否还有下一页
private boolean hasPrevious;// 是否还有上一页
private Collection<Integer> indexList;// 页码的集合
public NewsServices newsServices;

public int getTotalRows() {
return totalRows;
}

public NewsServices getNewsServices() {
return newsServices;
}

public void setNewsServices(NewsServices newsServices) {
this.newsServices = newsServices;
}

public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

public int getPageSize() {
return pageSize;
}

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

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public boolean isHasNext() {
return hasNext;
}

public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}

public boolean isHasPrevious() {
return hasPrevious;
}

public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}

public Collection<Integer> getIndexList() {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = 1; i <= getTotalPages(); i++)
result.add(new Integer(i));
return indexList;
}

public void setIndexList(Collection<Integer> indexList) {
this.indexList = indexList;
}

public static int getPage_size() {
return page_size;
}

public int getTotalPages() {
return totalPages;
}

public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}

public int getStartRow() {
if (getCurrentPage() == 1)
return 0;
else
return (getCurrentPage() - 1) * pageSize;
}

public void first() {
currentPage = 1;
}

public void last() {
currentPage = totalPages;
}

/*
* public void previous(){ if(currentPage==1) return; else currentPage--;
* } public void next(){ if(currentPage<totalPages) currentPage++; }
*/
public Collection<?> pageList(String countHsql, String queryHsql) {

try {
int totalRows = this.newsServices.getRows(countHsql);// 总行数
this.setTotalRows(totalRows);
int totalPages = totalRows / pageSize;// 总 页数
int mod = totalRows % pageSize;
if (mod > 0)
totalPages++;
if (currentPage == 0 || currentPage < 1) {
first();
}
if (currentPage > totalPages)
last();
if (currentPage >= 1 && currentPage <= totalPages)
hasNext = true;
if (currentPage > 1 && currentPage < totalPages)
hasPrevious = true;
return this.newsServices.getWithPage(queryHsql, getStartRow(),
currentPage*page_size);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

不知道你用不用的到
------解决方案--------------------
你一下子说了这么多,直接用hibernate提供的分页机制就行了
然后在jsp页面你会用到的struts分页相关标签:<logic:iterate id="User" scope="request" name="Userlist"></logic:iterate>这个是做遍历用;<logic:greaterThan value="1" name="currentpage"></logic:greaterThan>做比较用,不能让索引超出范围

------解决方案--------------------