|
// request.setAttribute("users", this.getFacade().getAll());
List<Users> users=(List)request.getSession().getAttribute("users");
if(users!=null)
{
int pageSize=2;
int pageCount=users.size()/pageSize+(users.size()%pageSize==0?0:1);
//得到当前页
int page=1;
String temp=request.getParameter("page");
if(temp!=null)
{
page=Integer.parseInt(temp);
}
//确定当前页的第一行
int pageRow=(page-1)*pageSize;
List<Users> pageRows=new ArrayList();
//通过循环取一页信息
for (int i = pageRow; i < pageRow+pageSize; i++)
{
if(i>=users.size())
{
break;
}
pageRows.add(users.get(i));
}
request.setAttribute("pageRows", pageRows);
//确定分页的html
String pageHtml="";
if(page!=1)
{
pageHtml="<a href='getPage.do?page=1'>第一页</a> ";
pageHtml=pageHtml+"<a href='getPage.do?page="+(page-1)+"'>上一页</a> ";
}
if(page!=pageCount)
{
pageHtml=pageHtml+"<a href='getPage.do?page="+(page+1)+"'>下一页</a> ";
pageHtml=pageHtml+"<a href='getPage.do?page="+pageCount+"'>最后一页</a> ";
}
//
String selectHtml="";
selectHtml="<select name='page' onchange='form1.submit();'>";
for (int i = 1; i <=pageCount; i++)
{
if(i==page)
{
selectHtml=selectHtml+"<option value='"+i+"' selected>"+i+"</option>";
}
else
{
selectHtml=selectHtml+"<option value='"+i+"'>"+i+"</option>";
}
}
selectHtml=selectHtml+"</select>";
request.setAttribute("pageHtml", pageHtml);
request.setAttribute("selectHtml", selectHtml);
} |
|