java-web分页操作(javaBean+Servlet+jsp)

Java-web分页操作(jsp+servlet+javaBean)

一 分页操作分析

分页在web项目中是非常重要的技术,打开每一个网页都可以看到分页

1.疑问的出现

在写分页前要了解什么是分页,分页一共有多少个方法、多少个参数,应该如何编写方法的实现和定义参数的变量

2.疑问的解决

分页一般分为首页、上一页、下一页、末页,还要得到总记录数,总页数,下面来详细介绍一下它们的概念

如果设当前页为newPage

(1)当前页 --------- 打开网页时看到的页面

(2)首页 ----------- 第一页 newPage=1

(3)上一页 --------- 当前页-1 newPage-1

(4)下一页 --------- 当前页+1 newPage+1

(5)末页 --------- 当前页==总页数 countPage=newPage

(6)总记录数 -------- select count(*) from 表名

(7)总页数 ---------

总记录数%每页显示的记录数=0 ? 总记录数/每页显示的记录数: 总记录数/每页显示的记录数+1

(8)显示当前页的分析 每页显示10条记录

第1页:newpage=1 起始记录为0 10

第2页:newpage=2 起始记录 10 10

第3页:newpage=3 起始记录 20 10

第4页:newpage=1 起始记录为30 10

第5页:newpage=2 起始记录 40 10

第6页:newpage=3 起始记录 50

第n页 newpage=n (newpage-1)*pageSize

(9)查询指定的页面

Select id,title,content from new limit 0,10

注:从0开始查询,每页显示10条记录

二 功能的实现

1.首先创建数据库

id name address 设置为int varchar varchar 类型的,并插入多条数据,以便于分页的操作

2.创建功能模块,先实现domain层 ----- 用于封装数据库中的数据,代码如下

package com.csdn.paging.domain;

public class Paging {

private Integer id;

private String name;

private String address;

public Paging() {

super();

}

public Paging(Integer id, String name, String address) {

super();

this.id = id;

this.name = name;

this.address = address;

}

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 getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public String toString() {

return "Paging [id=" + id + ", name=" + name + ", address=" + address

+ "]";

}

}

3.实现类的编写----对数据有条件的读取,其中包括查询指定页数、获取总记录数、获取总页数。

(1)创建一个接口,用于写这几个抽象方法 ---- PagingDao

package com.csdn.paging.dao;

import java.util.List;

import com.csdn.paging.domain.Paging;

public interface PagingDao {

//显示总的记录条数

Integer getCountRecord();

//根据当前页到结束页的查询

List<Paging> findIimitPage(Integer newPage);

//总的页数

Integer getCountPage();

}

(2)创建一个类,用于继承PagingDao接口 ---- PagingDaoImpl

package com.csdn.paging.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 com.csdn.paging.domain.Paging;

public class PagingDaoImpl implements PagingDao {

private static final Integer pageSize = 5;

private Integer countRecord;// 共有多少条记录

private Integer countPage;// 共有多少页

private static final String URL = "jdbc:mysql://localhost:3306/paging?user=root&password=qiao&useUnicode=true&characterEncoding=utf-8";

private static Connection conn;

private PreparedStatement pstmt;

private ResultSet rs;

static {

try {

// 加载驱动

Class.forName("com.mysql.jdbc.Driver");

// 建立连接

conn = DriverManager.getConnection(URL);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public Integer getCountRecord() {

// 设置返回值

Integer count = 0;

// 获取连接

// 定义sql语句 查询出一共的记录条数

String sql = "select count(*) as con from paging";

try {

// 创建预处理对象

pstmt = conn.prepareStatement(sql);

// 为占位符赋值

// 执行更新语句

rs = pstmt.executeQuery();

// 判断

if (rs.next()) {

count = rs.getInt("con");

}

// 计算出总页数,并从getCountPage方法中获取

this.countPage = ((count % pageSize) != 0 ? (count / pageSize + 1) : (count / pageSize));

// 释放资源

if (rs != null) {

rs.close();

}

if (pstmt != null) {

pstmt.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return count;

}

// 得到总的页数

public Integer getCountPage() {

// TODO Auto-generated method stub

return countPage;

}

// 根据传过来的数值条件查询

public List<Paging> findIimitPage(Integer newPage) {

// 修改返回值

List<Paging> entities = new ArrayList<Paging>();

// 获取连接

// 定义SQL语句

String sql = "select id,name,address from paging limit ?,?";

try {

// 创建预处理对象

pstmt = conn.prepareStatement(sql);

// 为占位符赋值

int index = 1;

pstmt.setObject(index++, (newPage - 1) * pageSize);

pstmt.setObject(index++, pageSize);

// 执行更新

rs = pstmt.executeQuery();

// 判断

while (rs.next()) {

Paging entity = new Paging();

entity.setId(rs.getInt("id"));

entity.setName(rs.getString("name"));

entity.setAddress(rs.getString("address"));

entities.add(entity);

}

// 释放资源

if (rs != null) {

rs.close();

}

if (pstmt != null) {

pstmt.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return entities;

}

4.布局显示的jsp页面,在此页面上给servlet传参数,并通过跳转页面返回到jsp页面,servlet层是业务逻辑层,用于处理和Dao的连接

Jsp页面 paging.jsp

注意导入标签

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<body>

//通过超链接连接到servlet层上,并为newPage传参数

<a href="${pageContext.request.contextPath}/paging?newPage=1">查看所有信息</a>

<table border="1">

<c:forEach items="${entities}" var="entity">

<tr>

<td>

${entity.id}

</td>

<td>

${entity.name}

</td>

<td>

${entity.address}

</td>

</tr>

</c:forEach>

</table>

<a href="${pageContext.request.contextPath}/paging?newPage=1">首页</a>

<a

href="${pageContext.request.contextPath}/paging?newPage=${newPage-1<=1?1:newPage-1}">上一页</a>

<a

href="${pageContext.request.contextPath}/paging?newPage=${newPage+1>=countPage?countPage:newPage+1}">下一页</a>

<a

href="${pageContext.request.contextPath}/paging?newPage=${countPage}">末页</a>

</body>

</html>

5.servlet层的使用 ---- PagingServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

String npage = request.getParameter("newPage");

List<Paging> entities = pageService.findIimitPage(new Integer(npage));

int countRecord = pageService.getCountRecord();

int countPage = pageService.getCountPage();

request.setAttribute("entities", entities);

request.setAttribute("countPage", countPage);

request.setAttribute("newPage", npage);

request.setAttribute("countRecord", countRecord);

request.getRequestDispatcher("./paging.jsp").forward(request, response);

}

效果如下图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值