页面的上下两个表单可以封装成一个jsp组件.实现代码的重用
具体实现流程如下:
客户端的jsp页面传送两个变量:
1. Keyword:要查询的关键字
2. currentPage:当前要显示第几页
servelt中通过调用impl类的方法返回如下内容:
1. ArrayList对象:里面存储的是对应的页的数据
2. 原样返回currentPage,单击”next”则在此基础之上++;存在(hidden中)
3. 原样返回Keyword,下次单击按钮的时候在重新把keyword传过去存在(hidden中)
4. 返回根据要搜索的内容,和每页要显示的数量,计算要显示的总页数存在(hidden中)
按钮的翻页效果通过js实现下面是next按钮实现代码:
window.open("/splitpage/SplitServelt?currentPage=" + (page+1) +"&keyword=" + keyword,"_self");
真分页要注意如下问题:
1. PreparedStatement 的"?"功能 只能充当where 条件后面的占位符不能和top一起使用
2. 所以在实现分页的时候用了Statement + 连接变量的方式完成
3. 分页代码如下:select top 5 * from person where id >(select max(id) from person where id in (select top ((3-1)*5) id from person where name like '刘%'))
分析如下:
代码1: select top ((3-1)*5) id from person where name like '刘%' 把前2页数据的id全部找出来
查询和”刘”相关的女儿,(3-1)*5 代表要显示的是第3页.每页显示5行数据
代码2: select max(id) from person where id in(select top ((3-1)*5) id from person where name like '刘%' )
重刚刚查询到的id里面选出最大值
代码3: select top 5 * from person where id >(select max(id) from person where id in (select top ((3-1)*5) id from person where name like '刘%'))
要显示5条记录.记录数的id 必须大于已经找出来的id(前两页的id)
1