action中
@SuppressWarnings({ "unchecked", "rawtypes" }) public String selectList(){ Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request"); try{ Map<String,Object> session = ActionContext.getContext().getSession(); List list = newsBiz.selectByPage("News", 1, 3); int totalPage = newsBiz.getTotal("News"); request.put("list", list); double tatalPages=totalPage/3.0; int totalPage1=(int)Math.ceil(tatalPages);//向上取整 总页数 request.put("totalPage", totalPage1); request.put("currentPage", 1); }catch(Exception e){ e.printStackTrace(); } return "selectList"; }
/**
* 分页查找下一页
* @return
*/
@SuppressWarnings("rawtypes")
public String selectNextPage(){
HttpServletRequest request = ServletActionContext.getRequest();
Map<String,Object> map = (Map<String, Object>) ActionContext.getContext().get("request");
int next = Integer.parseInt(request.getParameter("page"));
try{
List list = newsBiz.selectByPage("News", next, 3);
int totalPage = newsBiz.getTotal("News");
map.put("list", list);
double tatalPages=totalPage/3.0;
int totalPage1=(int)Math.ceil(tatalPages);
map.put("totalPage", totalPage1);
map.put("currentPage", next);
}catch(Exception e){
e.printStackTrace();
}
return "selectnextList";
}
dao层实现类
@Override public List<News> selectByPage(String className, int pageNo, int pageSize) { Query query = this.getSession().createQuery("from "+ className); query.setFirstResult((pageNo - 1) * pageSize);//从哪个地方开始查询 query.setMaxResults(pageSize);//每页显示的记录数 return query.list(); }
页面jsp
function show(next,total){
if (next<=0) {
alert("已经是首页了");
return;
}
if (next>total) {
alert("已经是尾页了");
return;
}
document.forms[0].action="newsselectNextPage?page="+next;
document.forms[0].submit();
}
<div> 第${currentPage }页 <input type="hidden" id="tpage" name="tpage" value="${totalPage} "></input> <c:if test="${currentPage>1 }"> <input type="button" value="首页" onclick="show(1)"></input> <input type="button" value="上一页" onclick="show(${currentPage-1})"></input> </c:if> <c:if test="${currentPage<totalPage }"> <input type="button" value="下一页" onclick="show(${currentPage+1},${totalPage })"></input> <input type="button" value="尾页" onclick="show(${totalPage})"></input> </c:if> 共${totalPage }页 </div>