需求说明:
• 演示最终分页效果
• 提供分页素材
 
 
• 分页的作用
• 数据量大,一页容不下
• 后台查询部分数据而不是全部数据
• 降低带宽使用,提高访问速度
 
 
• 分页的实现思路
• MVC四个层次都需要参与分页操作SXT SXT 练习2——理解PageBean
 
• 需求说明:
• 提供面向对象的PageBean,降低分页难度,实现功能重用
• 理解PageBean的属性和方法
 
 
• 分页的三个基本属性
• 1.每页几条记录size 可以有默认值5
• 2.当前页号 index 可以有默认值1
• 3.记录总数totalCount:没有默认值,需查询数据库获取真正记录总数
 
 
• 分页的其他属性
• 一共多少页 :totalPageCount=totalCount/size+1
• 上一页 index-1 当前页1,上一页1
• 下一页 index+1 当前页是最后一页 下一页:还是最后一页
 
 
• 扩展
• 分页Bean还可以放要查询的数据 protected List<T> list;
• 分页Bean还可以放页码列表 [1] 2 3 4 5 private int[] numbers;SXT SXT 练习2——理解PageBean
• public class PageBean<T> {
private int size = 5;//每页显示记录
private int index = 1;// 当前页号
private int totalPageCount = 1;// 总页数
private int totalCount = 0;// 记录总数
private int[] numbers;//展示页数集合
protected List<T> list;//要显示到页面的数据集
//赋值记录总数
public void setTotalCount(int totalCount) {}
//根据记录总数计算总页数
private void setTotalPageCountByRs() {}
//根据总页数计算页面显示的页号范围
public void setNumbers(int totalPageCount) {}
• }SXT SXT
 
 
练习2——理解PageBean
• 需要根据当前页和总页数的关系确定显示的页码
• 情况1:总页数21,当前14
• 9 10 11 12 13 [14] 15 16 17 18
• 情况2:总页数21,当前1,2,3,4
• 1 2 [3] 4 5 6 7 8 9 10
• 情况3:总页数21,当前19,20,21
• 12 13 14 15 16 17 [18] 19 20 21
• 情况4:总页数不够10页
• 1 2 [3] 4 5SXT SXT
 
 
练习3——实现基本分页的后台操作
• 需求说明:
• 控制层
• 获取当前页号
• 将当前页号给PageBean
• 传递PageBean到业务层
• 业务层
• 获取记录总数
• 使用记录总数计算PageBean其他属性值
• 调用数据访问层获取当前页数据并给PageBean
• 数据访问层
• 分页查询语句 String sql = "select * from (select rownum r,e2.* from "
+ "( select e.* from student e order by score desc) e2 "
+ "where rownum<="+end+" ) where r>"+startSXT SXT
 

 
练习4——实现基本分页的页面显示
• 需求说明:
• 视图层显示分页
• 使用JSTL/EL完成分页数据显示

 
 练习5——完善基本分页
 
• 需求说明:
• 数据访问层
• 理解分页查询语句:
 
• Oracle :复杂 三层子查询
 

SELECT u_id,m_id as id,m_date as pubTime,m_body as body,m_image as image,m_tranum as tranum,
m_comnum as comnum,m_colnum as colnum,m_like as likeN,to_char(m_date,'yyyy-mm-dd hh24:mi:ss') as str_pubTime FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM weibo_tab where u_id=#{param1} and m_state = 0 order by m_date desc) A
WHERE ROWNUM <= #{param3}
)
WHERE RN >= #{param2}

• MySQL:简单
 
select * from student limit 5,5//start,size
 
 
• 业务层
• 直接获取记录总数 select count (*) from student
• 视图层
• 改变每页记录数
• 直接跳到某一页
• 代码优化,提取JS方法
• 控制层
• 取当前页号和每页记录数
function change(index,size){
location.href="servlet/ShowAllServlet
?index="+index+"&size="+size;
}
 
 
 
练习6——带条件查询的分页
 
• 需求说明:
• 视图层:
• 查询表单
• 记忆查询条件
• 控制层:
• 获取表单数据,将表单数据(查询条件)传递到业务层
• 业务层:
• 获取符合查询条件的记录总数
• 获取符合查询条件的记录数据
• DAO层:
• 改变SQL查询语句,需要根据查询条件拼接SQL语句
 
 
 
 练习7——完善带条件查询的分页
 
• 需求说明:
• 点击页码超链接的同时要提交表单
• 实现1:修改form的action属性
• document.forms[0].action="servlet/ShowAllServlet?index="+index+"&size="+size;
• 实现2:给表单添加hidden属性,表示index和size
• <input type="hidden" id="index" name="index" >
• <input type="hidden" id="size" name="size" >
• document.getElementById("index").value=index;
• document.getElementById("size").value=size;
• 实现直接输入页号并提交
• 与上个功能类似
• 更新删除后仍旧跳回当前页,而不是第一页
 
 
详细代码如下:
 
pageBean.java
  1. package com.briup.common.util;
  2.  
  3. import java.util.List;
  4.  
  5. /**
  6. * 分页的三个基本属性
  7. * 1.每页几条记录size 可以有默认值5
  8. * 2.当前页号 index 可以有默认值1
  9. * 3.记录总数totalCount:不可能有默认值,需要查询数据库获取真正的记录总数
  10. *
  11. * 4.一共多少页 :totalPageCount=totalCount/size+1
  12. * 5 30 31 32 33 34 35
  13. * 5.上一页 index-1 当前页1,上一页1
  14. * 6.下一页 index+1 当前页是最后一页 下一页:还是最后一页
  15. *
  16. * 扩展
  17. * 分页Bean还可以放要查询的数据 protected List<T> list;
  18. * 分页Bean还可以放页码列表 [1] 2 3 4 5 private int[] numbers;
  19. *
  20. * @author Administrator
  21. *
  22. * @param <T>
  23. */
  24. public class PageBean<T> {
  25. private int size = ;//每页显示记录 //
  26. private int index = ;// 当前页号
  27. private int totalCount = ;// 记录总数 ok
  28.  
  29. private int totalPageCount = ;// 总页数 ok
  30.  
  31. private int[] numbers;//展示页数集合 //ok
  32. protected List<T> list;//要显示到页面的数据集
  33.  
  34. /**
  35. * 得到开始记录
  36. * @return
  37. */
  38. public int getStartRow() {
  39.  
  40. return (index - ) * size;
  41. }
  42.  
  43. /**
  44. * 得到结束记录
  45. * @return
  46. */
  47. public int getEndRow() {
  48.  
  49. return index * size;
  50. }
  51.  
  52. /**
  53. * @return Returns the size.
  54. */
  55. public int getSize() {
  56. return size;
  57. }
  58.  
  59. /**
  60. * @param size
  61. * The size to set.
  62. */
  63. public void setSize(int size) {
  64. if (size > ) {
  65. this.size = size;
  66. }
  67. }
  68. /**
  69. * @return Returns the currentPageNo.
  70. */
  71. public int getIndex() {
  72. if (totalPageCount == ) {
  73.  
  74. return ;
  75. }
  76.  
  77. return index;
  78. }
  79.  
  80. /**
  81. * @param currentPageNo
  82. * The currentPageNo to set.
  83. */
  84. public void setIndex(int index) {
  85. if (index > ) {
  86. this.index = index;
  87. }
  88. }
  89.  
  90. /**
  91. * @return Returns the totalCount.
  92. */
  93. public int getTotalCount() {
  94. return totalCount;
  95. }
  96.  
  97. /**
  98. * @param totalCount
  99. * The totalCount to set.
  100. */
  101. public void setTotalCount(int totalCount) {
  102. if (totalCount >= ) {
  103. this.totalCount = totalCount;
  104. setTotalPageCountByRs();//根据总记录数计算总页�?
  105. }
  106. }
  107.  
  108. public int getTotalPageCount() {
  109. return this.totalPageCount;
  110. }
  111.  
  112. /**
  113. * 根据总记录数计算总页�?
  114. * 5
  115. * 20 4
  116. * 23 5
  117. */
  118. private void setTotalPageCountByRs() {
  119. if (this.size > && this.totalCount > && this.totalCount % this.size == ) {
  120. this.totalPageCount = this.totalCount / this.size;
  121. } else if (this.size > && this.totalCount > && this.totalCount % this.size > ) {
  122. this.totalPageCount = (this.totalCount / this.size) + ;
  123. } else {
  124. this.totalPageCount = ;
  125. }
  126. setNumbers(totalPageCount);//获取展示页数集合
  127. }
  128.  
  129. public int[] getNumbers() {
  130. return numbers;
  131. }
  132.  
  133. /**
  134. * 设置显示页数集合
  135. *
  136. * 默认显示10个页码
  137. * 41 42 43 44 [45 ] 46 47 48 49 50
  138. *
  139. *
  140. * [1] 2 3 4 5 6 7 8 9 10
  141. *
  142. * 41 42 43 44 45 46 47 [48] 49 50
  143. * @param totalPageCount
  144. */
  145. public void setNumbers(int totalPageCount) {
  146. if(totalPageCount>){
  147. //!.当前数组的长度
  148. int[] numbers = new int[totalPageCount>?:totalPageCount];//页面要显示的页数集合
  149. int k =;
  150. //
  151. //1.数组长度<10 1 2 3 4 .... 7
  152. //2.数组长度>=10
  153. // 当前页<=6 1 2 3 4 10
  154. // 当前页>=总页数-5 ......12 13 14 15
  155. // 其他 5 6 7 8 9 当前页(10) 10 11 12 13
  156. for(int i = ;i < totalPageCount;i++){
  157. //保证当前页为集合的中�?
  158. if((i>=index- (numbers.length/+) || i >= totalPageCount-numbers.length) && k<numbers.length){
  159. numbers[k] = i+;
  160. k++;
  161. }else if(k>=numbers.length){
  162. break;
  163. }
  164. }
  165.  
  166. this.numbers = numbers;
  167. }
  168.  
  169. }
  170.  
  171. public void setNumbers(int[] numbers) {
  172. this.numbers = numbers;
  173. }
  174.  
  175. public List<T> getList() {
  176. return list;
  177. }
  178.  
  179. public void setList(List<T> list) {
  180. this.list = list;
  181. }
  182.  
  183. /*
  184. public static int getTotalPageCount(int iTotalRecordCount, int iPageSize) {
  185. if (iPageSize == 0) {
  186. return 0;
  187. } else {
  188. return (iTotalRecordCount % iPageSize) == 0 ? (iTotalRecordCount / iPageSize) : (iTotalRecordCount / iPageSize) + 1;
  189. }
  190. }*/
  191. }

pojo

Customer.java

  1. package com.briup.bean;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Customer implements Serializable{
  6. private static final long serialVersionUID = -1415977267636644567L;
  7. private Long id;
  8. private String name;
  9. private String password;
  10. private String zip;
  11. private String address;
  12. private String telephone;
  13. private String email;
  14. public Customer() {
  15. }
  16. public Customer(Long id, String name, String password, String zip,
  17. String address, String telephone, String email) {
  18. super();
  19. this.id = id;
  20. this.name = name;
  21. this.password = password;
  22. this.zip = zip;
  23. this.address = address;
  24. this.telephone = telephone;
  25. this.email = email;
  26. }
  27. public Customer(String name, String password, String zip,
  28. String address, String telephone, String email) {
  29. super();
  30.  
  31. this.name = name;
  32. this.password = password;
  33. this.zip = zip;
  34. this.address = address;
  35. this.telephone = telephone;
  36. this.email = email;
  37. }
  38. public Long getId() {
  39. return id;
  40. }
  41. public void setId(Long id) {
  42. this.id = id;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50. public String getPassword() {
  51. return password;
  52. }
  53. public void setPassword(String password) {
  54. this.password = password;
  55. }
  56. public String getZip() {
  57. return zip;
  58. }
  59. public void setZip(String zip) {
  60. this.zip = zip;
  61. }
  62. public String getAddress() {
  63. return address;
  64. }
  65. public void setAddress(String address) {
  66. this.address = address;
  67. }
  68. public String getTelephone() {
  69. return telephone;
  70. }
  71. public void setTelephone(String telephone) {
  72. this.telephone = telephone;
  73. }
  74. public String getEmail() {
  75. return email;
  76. }
  77. public void setEmail(String email) {
  78. this.email = email;
  79. }
  80. }

dao

  1. package com.briup.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import com.briup.bean.Customer;
  6. import com.briup.bean.Student;
  7.  
  8. public interface StudentDao {
  9. /**
  10. * ��ѯ����ѧ��
  11. * @return
  12. */
  13. public List<Customer> findAll();
  14. /**
  15. * ��ѯָ����Χ��ѧ��
  16. * @param start
  17. * @param end
  18. * @return
  19. */
  20. public List<Customer> findStu(int start, int size);
  21. /**
  22. * ��ѯ��¼����
  23. * @return
  24. */
  25. public int findCount();
  26. /**
  27. * ��ѯ��ϲ�ѯ�����ļ�¼����
  28. * @param name
  29. * @param minScore
  30. * @return
  31. */
  32. public int findCount(String name);
  33. /**
  34. * ��ѯ��ϲ�ѯ������ָ��ҳ���ѧ��
  35. * @param start
  36. * @param end
  37. * @param name
  38. * @param minScore
  39. * @return
  40. */
  41. public List<Customer> findStu(int start, int size, String name);
  42.  
  43. }

dao.impl

  1. package com.briup.dao.impl;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import com.briup.bean.Customer;
  12. import com.briup.bean.Student;
  13. import com.briup.common.util.DBUtil;
  14. import com.briup.dao.StudentDao;
  15.  
  16. public class StudentDaoImpl implements StudentDao {
  17. public List<Customer> findAll() {
  18. Connection conn =DBUtil.getConnection();
  19. Statement stmt =null;
  20. ResultSet rs =null;
  21. List<Customer> stuList = new ArrayList<Customer>();
  22. try {
  23. stmt =conn.createStatement();
  24. rs = stmt.executeQuery("select * from student");
  25. while(rs.next()){
  26. Customer cus1 = new Customer();
  27. cus1.setId(rs.getLong("customer_id"));
  28. cus1.setAddress(rs.getString("address"));
  29. cus1.setEmail(rs.getString("email"));
  30. cus1.setName(rs.getString("name"));
  31. cus1.setPassword(rs.getString("password"));
  32. cus1.setTelephone(rs.getString("telephone"));
  33. cus1.setZip(rs.getString("zip"));
  34. stuList.add(cus1);
  35. }
  36.  
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }finally{
  40. DBUtil.closeAll(rs, stmt, conn);
  41. }
  42. return stuList;
  43. }
  44.  
  45. public List<Customer> findStu(int start, int size) {
  46. Connection conn =DBUtil.getConnection();
  47. PreparedStatement pstmt =null;
  48. ResultSet rs =null;
  49. List <Customer> stuList = new ArrayList<Customer>();
  50. try {
  51. String sql = "select * from tbl_customer limit ?,?";
  52. pstmt =conn.prepareStatement(sql);
  53. pstmt.setInt(, start);
  54. pstmt.setInt(, size);
  55. rs = pstmt.executeQuery();
  56.  
  57. while(rs.next()){
  58. /*private Long id;
  59. private String name;
  60. private String password;
  61. private String zip;
  62. private String address;
  63. private String telephone;
  64. private String email;*/
  65. Customer cus1 = new Customer();
  66. cus1.setId(rs.getLong("customer_id"));
  67. cus1.setAddress(rs.getString("address"));
  68. cus1.setEmail(rs.getString("email"));
  69. cus1.setName(rs.getString("name"));
  70. cus1.setPassword(rs.getString("password"));
  71. cus1.setTelephone(rs.getString("telephone"));
  72. cus1.setZip(rs.getString("zip"));
  73.  
  74. stuList.add(cus1);
  75. }
  76.  
  77. } catch (SQLException e) {
  78. e.printStackTrace();
  79. }finally{
  80. DBUtil.closeAll(rs, pstmt, conn);
  81. }
  82. return stuList;
  83. }
  84.  
  85. public int findCount() {
  86. Connection conn =DBUtil.getConnection();
  87. Statement stmt =null;
  88. ResultSet rs =null;
  89. List <Student> stuList = new ArrayList<Student>();
  90. int count = ;
  91. try {
  92. stmt =conn.createStatement();
  93. rs = stmt.executeQuery("select count(*) from tbl_customer");
  94. rs.next();
  95. count = rs.getInt();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }finally{
  99. DBUtil.closeAll(rs, stmt, conn);
  100. }
  101.  
  102. return count;
  103. }
  104.  
  105. public int findCount(String name) {
  106. Connection conn =DBUtil.getConnection();
  107. Statement stmt =null;
  108.  
  109. ResultSet rs =null;
  110. List <Student> stuList = new ArrayList<Student>();
  111. int count = ;
  112. try {
  113. StringBuilder sql = new StringBuilder("select count(*) from tbl_customer where 1=1 ");
  114. if(name != null && !"".equals(name)){
  115. sql.append(" and name like '%"+name+"%'");
  116. }
  117.  
  118. stmt =conn.createStatement();
  119. rs = stmt.executeQuery(sql.toString());
  120. rs.next();
  121. count = rs.getInt();
  122. System.out.println("StudentDaoImpl.findCount()"+count);
  123. } catch (SQLException e) {
  124. e.printStackTrace();
  125. }finally{
  126. DBUtil.closeAll(rs, stmt, conn);
  127. }
  128.  
  129. return count;
  130. }
  131.  
  132. public List<Customer> findStu(int start, int size, String name
  133. ) {
  134. Connection conn =DBUtil.getConnection();
  135. Statement stmt =null;
  136. ResultSet rs =null;
  137. PreparedStatement pstmt=null;
  138. List<Customer> stuList = new ArrayList<Customer>();
  139. try {
  140. stmt =conn.createStatement();
  141. StringBuilder sql = new StringBuilder("select * from tbl_customer cus where 1=1 ");
  142. if(name != null && !"".equals(name)){
  143. sql.append(" and name like '%"+name+"%'");
  144. }
  145.  
  146. sql.append(" order by score desc");
  147.  
  148. String sql2 = "select * from tbl_customer limit ?,?";
  149.  
  150. pstmt = conn.prepareStatement(sql2);
  151. pstmt.setInt(, start);
  152. pstmt.setInt(, size);
  153. rs = pstmt.executeQuery();
  154. while(rs.next()){
  155. Customer cus1 = new Customer();
  156. cus1.setId(rs.getLong("customer_id"));
  157. cus1.setAddress(rs.getString("address"));
  158. cus1.setEmail(rs.getString("email"));
  159. cus1.setName(rs.getString("name"));
  160. cus1.setPassword(rs.getString("password"));
  161. cus1.setTelephone(rs.getString("telephone"));
  162. cus1.setZip(rs.getString("zip"));
  163. stuList.add(cus1);
  164. }
  165.  
  166. } catch (SQLException e) {
  167. e.printStackTrace();
  168. }finally{
  169. DBUtil.closeAll(rs, stmt, conn);
  170. }
  171. return stuList;
  172. }
  173. }

Service

  1. package com.briup.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.briup.bean.Customer;
  6. import com.briup.bean.Student;
  7. import com.briup.common.util.PageBean;
  8.  
  9. public interface StudentService {
  10.  
  11. /**
  12. * ��ѯ����ѧ��
  13. * @return
  14. */
  15. public List<Customer> findAll();
  16. /**
  17. * ��ɻ�ķ�ҳ�����������ѯ������
  18. * @param pageBean
  19. */
  20. public void findStu(PageBean<Customer> pageBean);
  21. /**
  22. * ��ɷ�ҳ���������ѯ������
  23. * @param pageBean
  24. */
  25. public void findStu(PageBean<Customer> pageBean, String name);
  26.  
  27. }

Service.impl

  1. package com.briup.service.impl;
  2.  
  3. import java.util.List;
  4.  
  5. import com.briup.bean.Customer;
  6. import com.briup.bean.Student;
  7. import com.briup.common.util.PageBean;
  8. import com.briup.dao.StudentDao;
  9. import com.briup.dao.impl.StudentDaoImpl;
  10. import com.briup.service.StudentService;
  11.  
  12. public class StudentServiceImpl implements StudentService {
  13.  
  14. private StudentDao stuDao = new StudentDaoImpl();
  15.  
  16. public List<Customer> findAll() {
  17. return this.stuDao.findAll();
  18. }
  19.  
  20. public void findStu(PageBean<Customer> pageBean) {
  21. //��ѯ��ݿ���ȡ��¼����
  22. //int totalCount = this.stuDao.findAll().size();//????
  23. int totalCount = this.stuDao.findCount();
  24. System.out.println("count="+totalCount);
  25. //ʹ�ü�¼�������PageBean�е���������(totalCount,totalPageCount,numbers)���Ͳ�list����
  26. pageBean.setTotalCount(totalCount);
  27.  
  28. //����DAO���ȡָ��ҳ��ѧ����ݣ�������pageBean��list����
  29. /*
  30. *ÿҳsize = 5����¼
  31. * �ڼ�ҳ ��ʼ��¼��>= �����¼��<= <
  32. * 1 0 4 5
  33. * 2 5 9 10
  34. * 3 10 14 15
  35. *
  36. * index (index-1)*size index*size
  37. *
  38. */
  39. //int start = (pageBean.getIndex()-1)*pageBean.getSize();
  40. //int end= pageBean.getIndex()*pageBean.getSize();
  41. int start = pageBean.getStartRow();
  42. int end = pageBean.getEndRow();
  43. int size = pageBean.getSize();
  44. List<Customer> list = this.stuDao.findStu(start,size);
  45. pageBean.setList(list);
  46.  
  47. }
  48.  
  49. public void findStu(PageBean<Customer> pageBean, String name) {
  50. //��ѯ��ݿ���ȡ��ϲ�ѯ�����ļ�¼����
  51. int totalCount = this.stuDao.findCount(name);
  52. System.out.println("count="+totalCount);
  53.  
  54. //ʹ�ü�¼�������PageBean�е���������(totalCount,totalPageCount,numbers)���Ͳ�list����
  55. pageBean.setTotalCount(totalCount);
  56.  
  57. //����DAO���ȡָ��ҳ��ѧ����ݣ�������pageBean��list����
  58. int start = pageBean.getStartRow();
  59. int end = pageBean.getEndRow();
  60. int size = pageBean.getSize();
  61. List<Customer> list = this.stuDao.findStu(start,size,name);
  62. pageBean.setList(list);
  63.  
  64. }
  65.  
  66. }

Servlet

  1. package com.briup.web.servlet;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import com.briup.bean.Customer;
  11. import com.briup.common.util.PageBean;
  12. import com.briup.service.StudentService;
  13. import com.briup.service.impl.StudentServiceImpl;
  14.  
  15. public class ShowAllServlet extends HttpServlet {
  16.  
  17. public void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19. request.setCharacterEncoding("utf-8");
  20.  
  21. String sindex = request.getParameter("index"); // null ""
  22. int index = ;
  23. System.out.println(index);
  24. try {
  25. index = Integer.parseInt(sindex);//"5"
  26. } catch (NumberFormatException e) {
  27. e.printStackTrace();
  28. }
  29.  
  30. String ssize = request.getParameter("size"); // null ""
  31. int size = ;
  32. try {
  33. size = Integer.parseInt(ssize);//"5"
  34. } catch (NumberFormatException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. String name = request.getParameter("name");
  39.  
  40. //
  41. PageBean<Customer> pageBean = new PageBean<Customer>();
  42. pageBean.setIndex(index);
  43. pageBean.setSize(size);
  44. StudentService stuService = new StudentServiceImpl();
  45. //List<Student> stuList = stuBiz.findAll();
  46. //stuService.findStu(pageBean);//����Ҫ����stuList����Ϊ����ҵ��㴦�?���е���ݶ���PageBean��
  47. stuService.findStu(pageBean,name);
  48. request.setAttribute("pageBean", pageBean);// !!!!!!!
  49. request.setAttribute("name", name);
  50.  
  51. // 3com.bjsxt
  52.  
  53. request.getRequestDispatcher("/jsp/showAll.jsp").forward(request,
  54. response);
  55. }
  56.  
  57. public void doPost(HttpServletRequest request, HttpServletResponse response)
  58. throws ServletException, IOException {
  59. this.doGet(request, response);
  60. }
  61.  
  62. }

jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3.  
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <base href="<%=basePath%>">
  13.  
  14. <title>查询并显示所有学生信息</title>
  15.  
  16. <meta http-equiv="pragma" content="no-cache">
  17. <meta http-equiv="cache-control" content="no-cache">
  18. <meta http-equiv="expires" content="">
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  20. <meta http-equiv="description" content="This is my page">
  21. <!--
  22. <link rel="stylesheet" type="text/css" href="styles.css">
  23. -->
  24. <script type="text/javascript">
  25. function changeIndex(index){
  26. location.href="stu?index="+index;
  27. }
  28.  
  29. function changeSize(size){
  30. //alert(size);
  31. location.href= "stu?size="+size;
  32. }
  33.  
  34. function change2(index,size){
  35. //location.href="servlet/ShowAllServlet?index="+index+"&size="+size;
  36. document.forms[].action="stu?index="+index+"&size="+size;
  37. document.forms[].submit();
  38. }
  39.  
  40. function change(index,size){
  41. document.forms[].index.value = index;
  42. //document.forms[0].size.value = size;
  43. document.getElementById("size").value = size;
  44. document.forms[].submit();
  45. }
  46.  
  47. </script>
  48. </head>
  49.  
  50. <body>
  51. <hr>
  52. <form action="stu" method="post" id="form1">
  53. <table align="center">
  54. <tr>
  55. <td>姓名</td>
  56. <td>
  57. <input type="text" name="name" value="${name }">
  58. <input type="hidden" name="index" value="">
  59. <input type="hidden" id="size" name="size">
  60. </td>
  61.  
  62. <td><input type="submit" value="提交"></td>
  63. </tr>
  64. </table>
  65. </form>
  66. <hr>
  67.  
  68. <!-- 显示所有学生 /stumanager/ -->
  69. <table align="center" border="" width="60%">
  70. <tr>
  71. <th>学生 编号</th>
  72. <th>学生姓名</th>
  73. <th>学生年龄</th>
  74. <th>学生成绩</th>
  75. <th>vs.index</th>
  76. <th>更新操作</th>
  77. <th>删除操作</th>
  78. </tr>
  79. <c:forEach items="${pageBean.list}" var="stu" varStatus="vs">
  80. <tr>
  81.  
  82. <td>${stu.id }</td>
  83. <td>${stu.name }</td>
  84. <td>${stu.password}</td>
  85. <td>${stu.zip }</td>
  86. <td>${stu.address }</td>
  87. <td>${stu.telephone }</td>
  88. <td>${stu.email }</td>
  89. <td>${vs.index }</td>
  90. <td><a href="/stumanager/servlet/StudentServlet?operate=preupdate&sid=${stu.id}">更新</a></td>
  91. <td><a href="/stumanager/servlet/StudentServlet?operate=delete&sid=${stu.id}">删除</a></td>
  92. </tr>
  93. </c:forEach>
  94. <tr>
  95. <td colspan="" align="center">
  96. <a href="javascript:change(1,${pageBean.size })">首页</a>
  97. <c:if test="${pageBean.index !=1 }">
  98. <a href="javascript:change(${pageBean.index-1 },${pageBean.size })">上一页 </a>
  99. </c:if>
  100. <c:if test="${pageBean.index ==1 }">
  101. 上一页
  102. </c:if>
  103. <c:forEach items="${pageBean.numbers }" var="num">
  104. <c:if test="${num ==pageBean.index }">
  105. [<a href="javascript:change(${num },${pageBean.size })">${num }</a>]
  106. </c:if>
  107. <c:if test="${num != pageBean.index }">
  108. <a href="javascript:change(${num },${pageBean.size })">${num }</a>
  109. </c:if>
  110. </c:forEach>
  111. <c:if test="${pageBean.index != pageBean.totalPageCount }">
  112. <a href="javascript:change(${pageBean.index+1 },${pageBean.size })">下一页</a>
  113. </c:if>
  114. <c:if test="${pageBean.index == pageBean.totalPageCount }">
  115. 下一页
  116. </c:if>
  117. <a href="javascript:change(${pageBean.totalPageCount },${pageBean.size })">末页</a>
  118. 每页
  119. <select onchange="change(${pageBean.index},this.value)">
  120. <c:forEach begin="" end="" step="" var="i">
  121. <c:if test="${i==pageBean.size }">
  122. <option value="${i }" selected="selected">${i }</option>
  123. </c:if>
  124. <c:if test="${i!=pageBean.size }">
  125. <option value="${i }">${i }</option>
  126. </c:if>
  127. </c:forEach>
  128. </select>
  129. 条记录
  130. 直接跳到第
  131. <select onchange="change(this.value,${pageBean.size})">
  132. <c:forEach begin="" end="${ pageBean.totalPageCount }" var="num">
  133. <c:if test="${num == pageBean.index }">
  134. <option value="${num }" selected="selected">${num }</option>
  135. </c:if>
  136. <c:if test="${num != pageBean.index }">
  137. <option value="${num }">${num }</option>
  138. </c:if>
  139. </c:forEach>
  140. </select>
  141.  
  142. 共${pageBean.totalCount }条记录
  143. </td>
  144. </tr>
  145. </table>
  146.  
  147. </body>
  148. </html>

分页细节分析见github。

传统servelt项目之分页操作的更多相关文章

  1. asp.net动态网站repeater控件使用及分页操作介绍

    asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...

  2. AngularJS进阶(二十六)实现分页操作

    JS实现分页操作 前言 项目开发过程中,进行查询操作时有可能会检索出大量的满足条件的查询结果.在一页中显示全部查询结果会降低用户的体验感,故需要实现分页显示效果.受前面"JS实现时间选择插件 ...

  3. 配置一个简单的传统SSM项目

    背景 我们知道,从2002年开始,Spring一直在飞速的发展,如今已经成为了在Java EE开发中的标准,早期的项目都是传统的Spring-SpringMVC-Mybatis项目,打成一个war包丢 ...

  4. Spring Boot学习笔记:传统maven项目与采用spring boot项目区别

    项目结构区别 传统的maven构建的项目结构如下: 用maven构建的采用springboot项目结构如下: 二者结构一致,区别如下:传统项目如果需要打成war包,需要在WEB-INF目录结构配置we ...

  5. 011PHP文件处理——文件处理 文件内容分页操作类

    <?php /** * 文件内容分页操作类: */ //访问地址:http://basicphp.com/006file/011.php?&page=1 class StrPage { ...

  6. MySQL中的分页操作结合python

    mysql中的分页操作结合python --分页: --方式1: ;-- 读取十行 , --从第十行读取 往后再读十行 --方式2: offset ; --从第二十行开始读取10行 -- 结合pyth ...

  7. vue中的分页操作

    首先,先看分页的代码: 这里还需要进行操作: 1.分页操作主要传递俩个数据,total和pagenum,一个显示当前页面共有多少条数据,一个是翻页后的操作,看列表的数据能不能跟着改变,在进页面发送请求 ...

  8. 携程 Apollo 配置中心传统 .NET 项目集成实践

    官方文档存在的问题 可能由于 Apollo 配置中心的客户端源码一直处于更新中,导致其相关文档有些跟不上节奏,部分文档写的不规范,很容易给做对接的新手朋友造成误导. 比如,我在参考如下两个文档使用传统 ...

  9. Vue-CLI 项目中相关操作

    0830总结 Vue-CLI 项目中相关操作 一.前台路由的基本工作流程 目录结构 |vue-proj | |src | | |components | | | |Nav.vue | | |views ...

随机推荐

  1. PHP 类型比较

    PHP 类型比较 虽然 PHP 是弱类型语言,但也需要明白变量类型及它们的意义,因为我们经常需要对 PHP 变量进行比较,包含松散和严格比较. 松散比较:使用两个等号 == 比较,只比较值,不比较类型 ...

  2. PHP curl_multi_remove_handle函数

    (PHP 5) curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源 说明 int curl_multi_remove_handle ( resource ...

  3. PHP soundex() 函数

    实例 计算 "Hello" 的 soundex 键: <?php高佣联盟 www.cgewang.com$str = "Hello";echo sound ...

  4. luogu P3645 [APIO2015]雅加达的摩天楼 分块 根号分治

    LINK:雅加达的摩天楼 容易想到设\(f_{i,j}\)表示第i个\(doge\)在第j层楼的最小步数. 转移显然是bfs.值得一提的是把初始某层的\(doge\)加入队列 然后转移边权全为1不需要 ...

  5. 使用VMware虚拟机建立Ubuntu与主机win7的文件共享与传输

    1.要想在虚拟机与主机之间建立共享文件夹必须先安装VMware Tools.方法见https://www.cnblogs.com/lsc666js/p/13403919.html. 2.在VMware ...

  6. 微信小程序--家庭记账小账本(四)

    今天的进展不太顺利,总的账单表,代码改了又改,最后决定用一个新的表,账单界面中弄了一天删除,发现都无法实现想要的效果,于是把账单界面的删除功能去了,就感觉大功告成的时候,发现收入和支出界面的删除也出现 ...

  7. 常哥带你认识NoSQL和Redis的强大

    各位朋友,这篇文章是针对Redis快速了解的内容,为了学好Redis在这里首先跟大家聊聊NoSQL相关内容,有了概念和方向后,我们再学习Redis大家会感觉得心应手. [公众号dotNet工控上位机: ...

  8. Unity3D制作类似吃鸡的小地图

    先看效果图: 实现的效果就是右上角的一个小地图,会随着人物的移动而移动,显示人物的方向,并且可以展示地图设定范围的其他的玩家 制作起来也很简单,不需要任何代码.主要原理就是先创建Render Text ...

  9. bluecms v1.6 代码审计

    0x01 使用seay源代码审计系统进行审计 扫描到了很多个可疑漏洞,不过工具都有一定的误报,下面我们就逐个进行验证 0x02 /ad_js.php SQL注入漏洞 查看源码,我们发现程序通过GET方 ...

  10. 总结vue知识体系之实用技巧

    vue 作为目前前端三大框架之一,对于前端开发者可以说是必备技能.那么怎么系统地学习和掌握 vue 呢?为此,我做了简单的知识体系体系总结,不足之处请各位大佬多多包涵和指正,如果喜欢的可以点个小赞!本 ...