分页技术

1 篇文章 0 订阅

```java

```java
import java.util.ArrayList;

public class PageBean<T> {
	//当前页
	private int currentPage;
	//当前页显示的条数
	private int currentCount;
	//总条数
	private int totalCount;
	//总页数
	private int totalPage;
	//每页显示的数据
	private ArrayList<T> productList=new ArrayList<T>();
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getCurrentCount() {
		return currentCount;
	}
	public void setCurrentCount(int currentCount) {
		this.currentCount = currentCount;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public ArrayList<T> getProductList() {
		return productList;
	}
	public void setProductList(ArrayList<T> productList) {
		this.productList = productList;
	}
	
}



import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Product;
import service.ProductService;
import vo.PageBean;

/**
 * Servlet implementation class ProductPageListServlet
 */
@WebServlet("/ProductPageListServlet")
public class ProductPageListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductPageListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductService service=new ProductService();
		PageBean <Product>pageBean=null;
		//模拟当前是第一页
		int currentPage=1;
		String _currentPage=request.getParameter("currentPage");
		if (_currentPage!=null) {
			currentPage=Integer.parseInt(_currentPage);
		}
		//默认每页5条
		int currentCount=5;
		try {
			pageBean=service.findPageBean(currentPage,currentCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("pageBean", pageBean);
		System.out.println(pageBean.getProductList());
		request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="domain.Product" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>JSP Page</title>
        <link rel="stylesheet" href="<%=request.getContextPath()%>/res/css/bootstrap.min.css" type="text/css" media="screen" />

        <script type="text/javascript">
        	function delProduct(id) {
        		var isDel=confirm("你确定要删除吗?");
        		if(isDel){
        			location.href="${pageContext.request.contextPath}/DelProductServlet?id="+id;
			}
        	}
        </script>
    </head>
    <body>
		<h3>条件查询</h3>

       
        <h3>书籍信息</h3>
        <table>
            <tr>
                <th>序号</th>
                <th>书名</th>
                <th>图片</th>
                <th>价格</th>
                <th>类别</th>
                <th>数量</th>
                <th>信息介绍</th>
                <th colspan="2">操作</th>
            </tr>
      
          
            
			 <c:forEach items="${pageBean.productList}" var="pro" varStatus="vs">
				<tr>
					<td>${vs.count}</td>
              		<td>${pro.name}</td>
              		<td><img width="40" height="45" src="${pageContext.request.contextPath}/${pro.imgurl}"/></td>
              		<td>${pro.price}</td>
              		<td>${pro.category}</td>
              		<td>${pro.pnum}</td>
              		<td>${pro.description}</td>
              		<td><a href="${pageContext.request.contextPath}/editProductServlet?id=${pro.id}">修改</a></td>
              		<td><a href="javascript:void(0);" onclick="delProduct('${pro.id}')">删除</a></td>
                </tr>
			</c:forEach>
	
        </table>
       	<!-- 分页 -->
        <table>
        	<tr>
        		<!-- 上一页 -->
				<c:if test="${pageBean.currentPage==1}">
					<td><a href="javascript:void(0);">上一页</a></td>
				</c:if> 
			 	<c:if test="${pageBean.currentPage!=1}">
					<td>
					<a href="${pageContext.request.contextPath}/ProductPageListServlet?currentPage=${pageBean.currentPage-1}">上一页</a>
					</td>
				</c:if>
				<!-- 循环输出页面号 -->
	            <c:forEach begin="1" end="${pageBean.totalPage}" var="pageNum">
	            	<c:if test="${pageNum==pageBean.currentPage}">
	            	<td>${pageNum}</td>
	            	</c:if>
	            	<c:if test="${pageNum!=pageBean.currentPage}">
	            	<td><a href="${pageContext.request.contextPath}/ProductPageListServlet?currentPage=${pageBean.currentPage}">${pageNum}</a></td>
	            	</c:if>
				</c:forEach>
			<!-- 下一页 -->
				<c:if test="${pageBean.currentPage==pageBean.totalPage||pageBean.totalPage==0}">
					<td><a href="javascript:void(0);">下一页</a></td>
				</c:if> 
			 	<c:if test="${pageBean.currentPage!=pageBean.totalPage&&pageBean.totalPage!=0}">
					<td>
					<a  href="${pageContext.request.contextPath}/ProductPageListServlet?currentPage=${pageBean.currentPage+1}">下一页</a>
					</td>
				</c:if>
			<tr>
		</table>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值