在jsp里面简单分页

来源:互联网 发布:绥化学院教务管理网络 编辑:程序博客网 时间:2024/05/21 14:08
有时候我们在后台传到的jsp里面的数据有很多,但是又不知道具体有多少,只能用循环来解决,但是一个页面放不下的时候,我们可能就需要进行分页,使每一个页面都有适量的数据
<%@page import="java.util.ArrayList"%><%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="com.model.*"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><jsp:include page="easyui.jsp"></jsp:include><%if (session.getAttribute("currentuser") == null) {//防止未登录用户直接进入界面response.sendRedirect("login.jsp");return;}%><script type="text/javascript">window.onload = function() {if (!sessionStorage.getItem("firstpageflag")) {sessionStorage.setItem("firstpageflag", 1);$.ajax({type : "post",async : false,url : "goods?action=GetGoods",datatype : "json",success : function(data) {window.location.reload();},})}}function divclick(temp) {var a = temp.getElementsByTagName("input")[0].value;var b = temp.getElementsByTagName("input")[1].value;var fm = document.getElementById("fm");fm.action = "goods?action=GetAppraise&classify=" + a + "&bid=" + b + "";fm.submit();}</script><style type="text/css">.firstpagea {display: inline-block;}.firstpagea:hover {background: red;}html body #FirstPagediv {width: 80%;height: 80%;position: relative;}</style></head><body><center><div class="FirstPagediv"><form id="fm" method="post"><!-- 负责这个界面的内容 --><%final int PAGESIZE = 8;//每页放8个商品;ArrayList<Goods> list = new ArrayList<Goods>();if (session.getAttribute("bookslist") != null) {//如果有这个商品的话list = (ArrayList<Goods>) session.getAttribute("bookslist");//这是获取后台的session数据,你可以获取你自己的数据}%><%int PageCount = 0;int CurrPage = 1;//当前页,首先设置为1;if (list.size() > 0) {//如果商品的数量不为0int Size = list.size();PageCount = (Size % PAGESIZE == 0) ? (Size / PAGESIZE) : (Size/ PAGESIZE + 1);//计算总共有多少个页面String tmp = request.getParameter("CurrPage");//获取带过来的页面数,比如你从第一页到第二页就需要传递这样的参数if (tmp == null) {//开始进入的时候当然获取不到页面数,就默认为第一页tmp = "1";}CurrPage = Integer.parseInt(tmp);CurrPage = CurrPage == 0 ? 1 : CurrPage;//防止在第一页点击上一页CurrPage = CurrPage > PageCount ? PageCount : CurrPage;//防止在最后一页点击下一页int count = (CurrPage - 1) * PAGESIZE;//当前页乘以也大小,计算要显示多少个for (int i = count; i < count + PAGESIZE && i < Size; i++)//{%><div style="float: left; width: 20%; margin-left: 5%"onclick="divclick(this)"><input type="hidden" value="<%=list.get(i).getBClassify()%>"><input type="hidden" value="<%=list.get(i).getBID()%>"> <imgsrc="booksimg/<%=list.get(i).getBID() + ".jpg"%>"><br><%-- <aclass="firstpagea" style="display: block"href="SecondPage.jsp?BClassify=<%=list.get(i).getBClassify()%>&BId=<%=list.get(i).getBID()%>"><%=list.get(i).getBName()%></a> --%><p><%=list.get(i).getBName()%></div><%}}%><div style="position: absolute; bottom: 50px; margin-left: 650px;"><div style="text-align: center"><a href="FirstPage.jsp?CurrPage=1">首页</a> <ahref="FirstPage.jsp?CurrPage=<%=CurrPage - 1%>">上一页</a> <ahref="FirstPage.jsp?CurrPage=<%=CurrPage + 1%>">下一页</a> <ahref="FirstPage.jsp?CurrPage=<%=CurrPage%>">尾页</a> 第<%=CurrPage%>页/共<%=PageCount%>页</div></div></form></div></center></body></html>