使用jsp-javabean-servlet实现分页功能

1、首页首先设置数据库:表名叫good,表的信息如下图:

入信息为:

2、展示项目结构:


3、第一步写jsp页面,也就是mvc模式的view视图层:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="./good.do"> <table width="600" border="2" cellspacing="0" cellpadding="0" height="10"> <tr> <td width="120" height="10" align="center"> 商品名称 </td> <td width="120" height="30" align="center"> 商品类型 </td> <td width="120" height="30" align="center"> 商品价格 </td> <td width="120" height="30" align="center"> 库存数量 </td> <td width="120" height="30" align="center"> 制造商 </td> </tr> <c:forEach var="good" items="${goodlist}"> <tr> <td width="120" height="10" align="center"> ${good.name} </td> <td width="120" height="10" align="center"> ${good.model} </td> <td width="120" height="10" align="center"> ${good.price} </td> <td width="120" height="10" align="center"> ${good.number} </td> <td width="120" height="10" align="center"> ${good.maker} </td> </tr> </c:forEach> </table> <a href="./good.do?currentpage=${1}">首页</a> <a href="./good.do?currentpage=${currentpage-1}">上一页</a> <a href="./good.do?currentpage=${currentpage+1}">下一页</a> <a href="./good.do?currentpage=${pagecount}">尾页</a> <br> 共有${pagecount} 页,当前页是${currentpage},共有${total}个商品 </form> </body> </html>
3、接下来写model层:

建一个javabean代码如下:

package cn.csdn.web.domain; import java.io.Serializable; public class Good implements Serializable { private Integer id; private String name; private String model; private Integer price; private Integer number; private String maker; public Good() { super(); // TODO Auto-generated constructor stub } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } public String getMaker() { return maker; } public void setMaker(String maker) { this.maker = maker; } }
接下来是写一些方法在一个接口中:

package cn.csdn.web.dao; import java.util.List; import cn.csdn.web.domain.Good; public interface GoodDao { List<Good> select(Integer nowpage); int pageCount(); }
最后实现这些方法:

package cn.csdn.web.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.csdn.web.domain.Good; public class GoodDaoImpl implements GoodDao { int pagesize = 3; public static int total = 0; private static Connection conn = null; private PreparedStatement pstmt = null; private ResultSet rs = null; private static final String URL = "jdbc:mysql://localhost:3306/3g?user=root&password=123&useUnicode=true&characterEncoding=UTF-8"; static { try { Class.forName("com.mysql.jdbc.Driver"); try { conn = DriverManager.getConnection(URL); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void release(ResultSet rs, PreparedStatement pstmt) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public List<Good> select(Integer nowpage) { List<Good> lists = new ArrayList<Good>(); String sql = "select id,name,model,price,number,maker from good limit ?,?"; try { pstmt = conn.prepareStatement(sql); int index = 1; pstmt.setInt(index++, (nowpage - 1) * pagesize); pstmt.setInt(index++, pagesize); rs = pstmt.executeQuery(); while (rs.next()) { Good entity = new Good(); entity.setId(rs.getInt("id")); entity.setName(rs.getString("name")); entity.setModel(rs.getString("model")); entity.setPrice(rs.getInt("price")); entity.setNumber(rs.getInt("number")); entity.setMaker(rs.getString("maker")); lists.add(entity); } } catch (SQLException e) { e.printStackTrace(); } release(rs, pstmt); return lists; } public int pageCount() { int pageCount = 0; String sql = "select count(*) from good"; try { pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()) { total = rs.getInt(1); pageCount = (total - 1) / pagesize + 1; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return pageCount; } } 5、最后实现mvc中的控制层control:

package cn.csdn.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.csdn.web.dao.GoodDao; import cn.csdn.web.dao.GoodDaoImpl; import cn.csdn.web.domain.Good; public class GoodServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); int currentpage = 1; GoodDao gDao = new GoodDaoImpl(); int pagecount = gDao.pageCount(); String page = request.getParameter("currentpage"); if (page != null) { currentpage = Integer.parseInt(page); if (currentpage <= 1) { currentpage = 1; } if (currentpage >= pagecount) { currentpage = pagecount; } } List<Good> GoodList = gDao.select(currentpage); request.setAttribute("goodlist", GoodList); request.setAttribute("currentpage", currentpage); request.setAttribute("pagecount", pagecount); // 总条数 int total = GoodDaoImpl.total; request.setAttribute("total", total); request.getRequestDispatcher("./index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
6、最后是实现的效果图:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值