使用分页显示列表

//使用分页
//1.计算总页数,
StudentBizImpl bb = new StudentBizImpl();
int pageSize = 5;//每页显示记录数
int count = bb.getStudentCount();//总记录数计算总页数
int totalpages= (count%pageSize ==0)?(count/pageSize):(count/pageSize+1);
//获取当前页
String currentPage = request.getParameter("pageIndex");
if(currentPage==null){
    currentPage = "1";
}
int pageIndex = Integer.parseInt(currentPage);//强转为int,便于计算
//计算开始索引
if(pageIndex < 1){
    pageIndex = 1;
}else if(pageIndex > totalpages){
    pageIndex = totalpages;
}
int stateIndex = pageSize*(pageIndex-1)+1;
//结束
int endIndex = pageSize*pageIndex;
String search ="";
if(request.getParameter("search")!=null){
    search = request.getParameter("search");
}
List<Student> studentList = bb.getList(stateIndex,endIndex);
request.setAttribute("studentList",studentList);
request.setAttribute("pageIndex",pageIndex);
request.setAttribute("totalpages",totalpages);
request.getRequestDispatcher("studentList.jsp").forward(request,response);

  

上一篇:JSP基本的语法、3个编译指令、7个动作指令、9个内置对象


下一篇:Freemarker的简单使用