JavaWEB九:thymeleaf 之 分页显示和关键字查询

Thymeleaf二:增删改查扩展

一、分页显示
  1. 在全查询的基础上,增加了一个参数。

    重点是控制首页、上一页、下一页、尾页的可点击情况

  2. DAO的实现类
    public class ListIpm extends BaseDAO<Customers> implements ListDAO{
         
         
        
        // 分页显示
        @Override
        public List<Customers> getList(Connection conn ,int pageOn) {
         
         
            String sql = "select cust_name name,cust_email email,cust_birth birth,cust_id id from customers limit ? , 2";
            return super.CommonRetrieve(conn, sql,(pageOn-1)*2);
        }
        
    	// 统计客户数量
        @Override
        public Long getCount(Connection conn) {
         
         
            String sql = "select count(*) from customers";
            return super.getValue(conn, sql);
        }
    }
    
  3. Servlet组件
    @WebServlet("/index")
    public class IndexServlet extends ViewBaseServlet {
         
         
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
         
         
            Connection conn = null;
            try {
         
         
                // 1.声明并初始化pageOn,是为了在js文件中page参数为空时,确保heml页面有具体的分页显示
                int pageOn = 2;
                // 2.count变量的类型必须是Long,应为SQL语言中的count()函数返回值类型是Bigint
                long count = 0;
                // 3.连接数据库,获取数据库中的数据,返回一个集合
                conn = JdbcUtils.getConnection();
                ListIpm ipm = new ListIpm();
                // 4.获取按钮被触发后,从html文件中传送到js文件内的页面值
                String pageStr = req.getParameter("page");
                if (StringUtil.isNotEmpty(pageStr)) {
         
         
                    pageOn = Integer.parseInt(pageStr);
                }
                // 5.获取该分页面上的客户信息列表
                List<Customers> custList = ipm.getList(conn,pageOn);
                // 6.获取客户个数,用于计算尾页的页号
                count = ipm.getCount(conn);
                int max = (int) (count+2-1)/2;
                // 7.将需要在html文件内调用的变量(数据)以键值对的形式保存到session作用域中,
                HttpSession session = req.getSession();
                session.setAttribute("cl",custList);
                session.setAttribute("page",pageOn);
                session.setAttribute("max",max);
                // 8.调用父类的模板处理方法,组装对应的html文件
                super.processTemplate("index",req,resp);
            } catch (Exception e) {
         
         
                e.printStackTrace();
            } finally {
         
         
                JdbcUtils.closeResource(conn,null);
            }
        }
    }
    
    
  4. html文件
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <html>
        <head>
            <meta charset="UTF-8">
            <base href="http://localhost:8080/page/" >
            <link rel="stylesheet" href="CSS/index.css" >
            <script language="JavaScript" th:src="@{js/index.js}" ></script>
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e_nanxu

感恩每一份鼓励-相逢何必曾相识

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值