本次小程序中,实现常见的java web页面中的分页效果。主要包括总共多少页/共多少页,首页,尾页,上一页,下一页,以及每次显示10条的页面序列号。具体代码如下:
第一步:model层的实现,新建一个Product对象,用来展示产品的信息。具体代码如下:
public class Product {
public static final int PAGE_SIZE=2;//表示每次只显示2条记录
private int id;
private String name;
private double price;
private int num;//产品库存量
private String unit;//产地或者供应商名字
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public static int getPageSize() {
return PAGE_SIZE;
}
}
第二步:数据库DAO层的一些实现:新建一个对象ProductDao,完成数据库的一些操作,比如建立连接,查询对应页号的数据,计算总页数等。代码如下:
public class ProductDao {
/*
* 用于创建数据库连接Connection对象
*/
public Connection getConnection(){
Connection conn=null;//数据库连接
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/你数据库名称";
String username="root";
String password="123456";
conn=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
/*
* 创建商品信息的分页查询方法fund(),该方法包含了一个page参数,
* 用于传递要查询的源码
*/
public List<Product> find(int page){
List<Product> list=new ArrayList<Product>();
Connection conn=getConnection();
String sql="select * from tb_product order by id limit ?,?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, (page-1)*Product.PAGE_SIZE);
ps.setInt(2, Product.PAGE_SIZE);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Product p=new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setNum(rs.getInt("num"));
p.setPrice(rs.getDouble("price"));
p.setUnit(rs.getString("unit"));
list.add(p);
}
rs.close();
ps.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
return list;
}
/*
* 获取商品信息的总记录数,用于计算商品信息的总页数
*/
public int findCount(){
int count=0;
Connection conn=getConnection();
String sql="select count(*) from tb_product";
try{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){//有数据
count=rs.getInt(1);
}
rs.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
return count;
}
}
第三部:控制层(C层)的实现,新建一个FindServlet,对每次打开之后页面处理的操作,并生成对应的页码序列号,发给前端V层。具体代码如下:
public class FindServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int currPage=1;
System.out.println(request.getParameter("page"));
if(request.getParameter("page")!=null){
currPage=Integer.parseInt(request.getParameter("page"));
}else{
currPage=1;
}
ProductDao dao=new ProductDao();
List<Product> list=dao.find(currPage);
request.setAttribute("list", list);
int pages;
int count=dao.findCount();
if(count%Product.PAGE_SIZE==0){
pages=count/Product.PAGE_SIZE;
}else{
pages=count/Product.PAGE_SIZE+1;
}
StringBuffer sb=new StringBuffer();
sb.append("第"+currPage+"页/共"+pages+"页");
sb.append(" ");
sb.append("<a href='FindServlet?page="+1+"'>"+"首页"+"</a>");
sb.append(" ");
if(currPage!=1){
sb.append("<a href='FindServlet?page="+(currPage-1)+"'>"+"上一页"+"</a>");
}
int begin=1,end=10;
//如果总页数不足10页,就把所有的页数显示出来
if(pages<=10){
begin=1;
end=pages;
}else{ //当页数大于10的时候,通过公式计算begin和end
begin=currPage-5;
end=currPage+4;
if(begin<1){//头溢出判断
begin=1;
end=10;//头溢出时尾部页码也要跟着改变
}
if(end>pages){//尾溢出
end=pages;
begin=end-9;//同理
}
}
//循环遍历页码列表
for(int i=begin;i<=end;i++){
if(i==currPage){
sb.append("["+i+"]");
}else{
sb.append("<a href='FindServlet?page="+i+"'>"+i+"</a>");
}
sb.append(" ");
}
if(currPage!=pages){
sb.append("<a href='FindServlet?page="+(currPage+1)+"'>"+"下一页"+"</a>");
}
sb.append("<a href='FindServlet?page="+pages+"'>"+"尾页"+"</a>");
request.setAttribute("bar", sb.toString());
request.getRequestDispatcher("product_list.jsp").forward(request,response);
}
}
第4步,前端V层的实现,新建一个名字叫product_list.jsp页面,具体布局代码如下:
<%@page import="com.cn.jdbc.Product"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'product_list.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>
<table align="center" width="450" border="1">
<tr>
<td align="center" colspan="5">
<h2>所有商品信息</h2>
</td>
</tr>
<tr align="center">
<td><b>ID</b></td>
<td><b>商品名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>单位</b></td>
</tr>
<%
List<Product> list=(List<Product>)request.getAttribute("list");
for(Product p:list){
%>
<tr align="center">
<td><%=p.getId()%></td>
<td><%=p.getName()%></td>
<td><%=p.getPrice()%></td>
<td><%=p.getNum()%></td>
<td><%=p.getUnit()%></td>
</tr>
<%
}
%>
<tr>
<td align="center" colspan="5">
<%=request.getAttribute("bar") %>
</td>
</tr>
</table>
</body>
</html>