Thymeleaf二:增删改查扩展
一、分页显示
-
在全查询的基础上,增加了一个参数。
重点是控制首页、上一页、下一页、尾页的可点击情况
-
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); } }
-
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); } } }
-
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>