public int getPageCount() {
// 计算出总页数
pageCount = (rowCount + pageSize - 1) / pageSize;
return pageCount;
}
public int getBeginPosition() {
// 计算出页起始
beginPosition = (currentPage - 1) * pageSize + 1;
return beginPosition;
}
public int getEndPosition() {
// 计算出页终止
if (currentPage >= pageCount) {
endPosition = rowCount;
} else {
endPosition = currentPage * pageSize;
}
return endPosition;
}
public boolean isHasNextPage() {
// 计算出是否有下一页
if (currentPage >= pageCount) {
hasNextPage = false;
} else {
hasNextPage = true;
}
return hasNextPage;
}
public boolean isHasPreviousPage() {
// 计算出是否有上一页
if (currentPage <= 1) {
hasPreviousPage = false;
} else {
hasPreviousPage = true;
}
return hasPreviousPage;
}
public int getFirstPage() {
return 1;
}
public int getPreviousPage() {
if (this.isHasPreviousPage()) {
return currentPage - 1;
}
return currentPage;
}
public int getNextPage() {
if (this.isHasNextPage()) {
return currentPage + 1;
} else {
return currentPage;
}
}
public int getLastPage() {
return pageCount;
}
/**
* 获取页数据
*
* @return ArrayList
*/
public ArrayList getArrayList() {
ArrayList list = new ArrayList();
for (int i = beginPosition; i <= endPosition; i++) {
list.add(arrayList.get(i - 1));
}
return list;
}
}
接续
package com.ali;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.ali.db.User;
import com.ali.util.PageUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserListController extends SimpleFormController {
private IIbatisDAO currDAO;
private String m_pagesize;
public UserListController() {
setCommandClass(UserListForm.class);
}
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if ( request.getMethod().equals("GET"))
{
//处理 http get请求
System.out.println("do Get!!");
UserListForm form = new UserListForm();
form.setPage("1");
form.setPageSize(getPageSize());
System.out.println("handleRequestInternal");
return onSubmit(request, response, form, null);
}
else
{
//return onSubmit(request, response, null, null);
System.out.println("do Post!!");
return super.handleRequestInternal(request, response);
}
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
//处理http post请求
UserListForm form = (UserListForm) command;
System.out.println("user list:" + form);
List list = this.getDAO().findUsers();
ArrayList arrList = new ArrayList();
for (Iterator i = list.iterator(); i.hasNext();) {
User user = (User) i.next();
System.out.println(user);
arrList.add(user);
}
PageUtil util = new PageUtil(arrList, form.getPageSize(), form
.getPage());
System.out.println("list.size=" + arrList.size());
request.setAttribute("userlist", util.getArrayList());
request.setAttribute("pager", util);
return new ModelAndView(this.getSuccessView());
}
public void setDAO(IIbatisDAO dao) {
currDAO = dao;
}
public IIbatisDAO getDAO() {
return currDAO;
}
public String getPageSize()
{
return m_pagesize;
}
public void setPageSize(String pagesize)
{
m_pagesize = pagesize;
}
}
具体的velocity内容
<BODY >
<form name="userlist" method="post" action="/spring/userlist.do">
<input type="hidden" name="page" >
<input type="hidden" name="pageSize" value=2>
</form>
<table width="772" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>
<table width="750" border="0" align="center" cellpadding="5" cellspacing="1">
<tr align="center">
<td background="images/q_12.gif"><span class="ff">用户名</span></td>
<td background="images/q_12.gif" class="ff">站点</td>
<td background="images/q_12.gif"><span class="ff">邮箱</span></td>
</tr>
#foreach($user in $userlist)
#set($color="E9F4FF")
#if($velocityCount%2==0)
#set($color="FFFFFF")
#end
<tr align="center" bgcolor="$color">
<td class="ii">$user.getUsername()</td>
<td>$user.getSite()</td>
<td class="style28">$user.getEmail()</td>
</tr>
#end
</table>
#set($pageFirst=1)
#if ($pager)
#set($pageEnd=$pager.getPageCount())
#set($pagePrevious=$pager.getPreviousPage())
#set($pageNext = $pager.getNextPage())
#else
#set($pageEnd=1)
#set($pagePrevious=1)
#set($pageNext =1)
#end
<scrīpt>
function goto(p) {
userlist.page.value=p;
userlist.pageSize.value=2;
userlist.submit();
}
</scrīpt>
<table width="100%" border="0">
<tr>
<td width="98%" height="43" align="right">
#if ($pager)
总 $pager.rowCount 条
第 $pager.currentPage/$pager.pageCount 页
#else
总 0 条
第 0 页
#end
<a href="#$pageFirst" ōnclick="goto($pageFirst)">首页</a>
<a href="#$pagePrevious" ōnclick="goto($pagePrevious)">上一页</a>
<a href="#$pageNext" ōnclick="goto($pageNext)">下一页</a>
<a href="#$pageEnd" ōnclick="goto($pageEnd)">末页</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</BODY>