一,改造
1.1通用分页核心思路将上一次查询请求再发一次,只不过页码变了
1.2将查询参数保存到pagebean中Map paramMap 存储参数键值对
1.3在pagebean定义一个属性存放上一次的请求
新建一 个外包package package在企业级命名不能为Servlet 最好使用web
package com.GeneralPaging.web;
BookServlet
代码:
1 package com.GeneralPaging.web; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.util.List; 6 import java.util.Map; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.GeneralPaging.dao.BookDao; 14 import com.GeneralPaging.entity.Book; 15 import com.GeneralPaging.util.PageBean; 16 17 public class BookServlet extends HttpServlet { 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = -5342105797517938272L; 23 24 private BookDao bookDao=new BookDao(); 25 26 @Override 27 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 // TODO Auto-generated method stub 29 doPost(req, resp); 30 } 31 32 @Override 33 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 //前台传入banme 36 String bname=req.getParameter("bname"); 37 bname=new String(bname.getBytes("ISO-8859-1"),"UTF-8"); 38 Book book=new Book(); 39 book.setBname(bname); 40 41 //分页 42 PageBean pagebean=new PageBean(); 43 44 try { 45 pagebean.setRequest(req); 46 List<Book> list = this.bookDao.list(book, pagebean); 47 req.setAttribute("bookList", list); 48 req.setAttribute("pageBean", pagebean); 49 req.getRequestDispatcher("/bookList.jsp").forward(req, resp); 50 } catch (InstantiationException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } catch (IllegalAccessException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } catch (SQLException e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } 60 super.doPost(req, resp); 61 } 62 63 }
配置BookServlet的xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>JavaGeneralPaging</display-name> <servlet> <servlet-name>bookServlet</servlet-name> <servlet-class>com.GeneralPaging.web.BookServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>bookServlet</servlet-name> <url-pattern>/bookServlet.action</url-pattern> </servlet-mapping> </web-app>
乱码处理,在EncodingFiter工具类中添加一个@WebFilter(urlPatterns=("/*"))则不需要配置xml
1 package com.GeneralPaging.util; 2 3 import java.io.IOException; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Set; 7 8 import javax.servlet.Filter; 9 import javax.servlet.FilterChain; 10 import javax.servlet.FilterConfig; 11 import javax.servlet.ServletException; 12 import javax.servlet.ServletRequest; 13 import javax.servlet.ServletResponse; 14 import javax.servlet.annotation.WebFilter; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 /** 19 * 中文乱码处理 20 * 21 */ 22 @WebFilter(urlPatterns=("/*")) 23 public class EncodingFiter implements Filter { 24 25 private String encoding = "UTF-8";// 默认字符集 26 27 public EncodingFiter() { 28 super(); 29 } 30 31 public void destroy() { 32 } 33 34 public void doFilter(ServletRequest request, ServletResponse response, 35 FilterChain chain) throws IOException, ServletException { 36 HttpServletRequest req = (HttpServletRequest) request; 37 HttpServletResponse res = (HttpServletResponse) response; 38 39 // 中文处理必须放到 chain.doFilter(request, response)方法前面 40 res.setContentType("text/html;charset=" + this.encoding); 41 if (req.getMethod().equalsIgnoreCase("post")) { 42 req.setCharacterEncoding(this.encoding); 43 } else { 44 Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合 45 Set set = map.keySet();// 取出所有参数名 46 Iterator it = set.iterator(); 47 while (it.hasNext()) { 48 String name = (String) it.next(); 49 String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组] 50 for (int i = 0; i < values.length; i++) { 51 values[i] = new String(values[i].getBytes("ISO-8859-1"), 52 this.encoding); 53 } 54 } 55 } 56 57 chain.doFilter(request, response); 58 } 59 60 public void init(FilterConfig filterConfig) throws ServletException { 61 String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集 62 if (null != s && !s.trim().equals("")) { 63 this.encoding = s.trim(); 64 } 65 } 66 67 }
改造PageBean
- 保存上一次请求携带的参数
- 控制是否分页
- 控制一页显示多少条数据
- 重载
- 最大页码数
- 获取下一页码数
- 获取上一页码数
1 package com.GeneralPaging.util; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 /** 9 * 分页工具类 10 * 11 */ 12 public class PageBean { 13 14 private int page = 1;// 页码 15 16 private int rows = 10;// 页大小 17 18 private int total = 0;// 总记录数 19 20 private boolean pagination = true;// 是否分页 21 22 private Map<String, String[]> paMap=new HashMap<>(); 23 24 private String url; 25 26 public void setRequest(HttpServletRequest req) { 27 //保存上一次请求携带的参数 28 this.setPaMap(req.getParameterMap()); 29 this.setUrl(req.getRequestURL().toString()); 30 31 //控制是否分页 32 this.setPagination(req.getParameter("pagination")); 33 // 控制一页显示多少条数据 34 this.setRows(req.getParameter("rows")); 35 this.setPage(req.getParameter("page")); 36 } 37 38 //重载 39 private void setPage(String page) { 40 // TODO Auto-generated method stub 41 this.page=StringUtils.isNotBlank(page) ? Integer.valueOf(page) : this.page; 42 } 43 44 private void setPagination(String pagination) { 45 // TODO Auto-generated method stub 46 this.pagination=StringUtils.isNotBlank(pagination) ? !"false".equals(pagination) : this.pagination; 47 } 48 private void setRows(String rows) { 49 // TODO Auto-generated method stub 50 this.rows=StringUtils.isNotBlank(rows) ? Integer.valueOf(rows) : this.rows; 51 } 52 53 54 public Map<String, String[]> getPaMap() { 55 return paMap; 56 } 57 58 public void setPaMap(Map<String, String[]> paMap) { 59 this.paMap = paMap; 60 } 61 62 public String getUrl() { 63 return url; 64 } 65 66 public void setUrl(String url) { 67 this.url = url; 68 } 69 70 public PageBean() { 71 super(); 72 } 73 74 public int getPage() { 75 return page; 76 } 77 78 public void setPage(int page) { 79 this.page = page; 80 } 81 82 public int getRows() { 83 return rows; 84 } 85 86 public void setRows(int rows) { 87 this.rows = rows; 88 } 89 90 public int getTotal() { 91 return total; 92 } 93 94 public void setTotal(int total) { 95 this.total = total; 96 } 97 98 public void setTotal(String total) { 99 this.total = Integer.parseInt(total); 100 } 101 102 public boolean isPagination() { 103 return pagination; 104 } 105 106 public void setPagination(boolean pagination) { 107 this.pagination = pagination; 108 } 109 110 /** 111 * 获得起始记录的下标 112 * 113 * @return 114 */ 115 public int getStartIndex() { 116 return (this.page - 1) * this.rows; 117 } 118 119 @Override 120 public String toString() { 121 return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; 122 } 123 124 //最大页码数 125 public int getMaxPage() { 126 return this.total % this.rows ==0 ? this.total/this.rows : this.total/this.rows+1; 127 } 128 129 /** 130 * 获取下一页码数 131 * @return 132 */ 133 public int getNextPage() { 134 return this.page< this.getMaxPage() ? this.page+1 : this.page; 135 136 } 137 138 /** 139 * 获取上一页码数 140 * @return 141 */ 142 public int getPreviousPage() { 143 return this.page > 1 ? this.page-1 : this.page; 144 } 145 146 }
2.通用分页标签编写
具体思路:
1、补全servlet
2、页面展示
3、分页重要参数(page、rows、是否分页、上一次请求、上一次的表单参数)
4、自定义分页标签
先写一个自定义分页标签
PageTag(助手类)
- 拼接下一次发送请求所要提交的form表单
- 上一次请求可能携带页码name=page的参数,但是该参数在前面已经单独赋值
- 为什么单独赋值?因为上一次请求时第一页的数据,下一次可能是第二页,意味着前后请求page对应的值是不一样的,需要单独赋值
- 下一次请求提交到后台的表单html代码拼接
- 拼接分页所需要调用的js代码
代码:
1 package com.JavaGeneralPaging.tag; 2 3 import java.io.IOException; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 import javax.servlet.jsp.JspException; 9 import javax.servlet.jsp.JspWriter; 10 import javax.servlet.jsp.tagext.BodyTagSupport; 11 12 import com.GeneralPaging.util.PageBean; 13 14 public class PageTag extends BodyTagSupport { 15 16 /** 17 * 18 */ 19 private static final long serialVersionUID = 46803535500945741L; 20 21 private PageBean pageBean; 22 23 24 public PageBean getPageBean() { 25 return pageBean; 26 } 27 28 public void setPageBean(PageBean pageBean) { 29 this.pageBean = pageBean; 30 } 31 32 @Override 33 public int doStartTag() throws JspException { 34 // TODO Auto-generated method stub 35 JspWriter out = pageContext.getOut(); 36 try { 37 out.print(toHTML()); 38 } catch (IOException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 return super.doStartTag(); 43 } 44 private String toHTML() { 45 StringBuilder sb=new StringBuilder(); 46 //拼接下一次发送请求所要提交的form表单 47 //下一次请求提交到后台的表单html代码拼接 48 sb.append(" <form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>"); 49 Map<String, String[]> paramMap = pageBean.getPaMap(); 50 if(paramMap != null &¶mMap.size() >0) { 51 // Set<Entry<String,String[]>> entrySet = paramMap.entrySet(); 52 //上一次请求可能携带页码name=page的参数,但是该参数在前面已经单独赋值 53 // 为什么单独赋值? 54 //因为上一次请求时第一页的数据,下一次可能是第二页,意味着前后请求page对应的值是不一样的,需要单独赋值 55 for(Entry<String,String[]> entry :paramMap.entrySet()) { 56 if(!"page".equals(entry.getKey())) { 57 for(String val : entry.getValue()) { 58 sb.append(" <input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>"); 59 } 60 } 61 } 62 } 63 sb.append(" <input type='hidden' name='page'>"); 64 sb.append(" </form>"); 65 66 //分页条html代码拼接 67 sb.append(" <div style='text-align: right; font-size: 12px;'>"); 68 sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a "); 69 sb.append(" href='javascript:gotoPage(1)'>首页</a> <a"); 70 71 sb.append(" href='javascript:gotoPage("+pageBean.getPreviousPage()+")'>上一页</a> <a"); 72 sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a> <a"); 73 sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a> <input type='text'"); 74 sb.append(" id='skipPage'"); 75 sb.append(" style='text-align: center; font-size: 12px; 50px;'> <a"); 76 sb.append(" href='javascript:skipPage()'>Go</a>"); 77 sb.append(" </div>"); 78 79 80 //拼接分页所需要调用的js代码 81 sb.append("<script type='text/javascript'>"); 82 sb.append(" function gotoPage(page) {"); 83 sb.append(" document.getElementById('pageBeanForm').page.value = page;"); 84 sb.append(" document.getElementById('pageBeanForm').submit();"); 85 sb.append(" }"); 86 sb.append(" function skipPage() {"); 87 sb.append(" var page = document.getElementById('skipPage').value;"); 88 sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){"); 89 sb.append(" alert('请输入1~N的数字');"); 90 sb.append(" return;"); 91 sb.append(" }"); 92 sb.append(" gotoPage(page);"); 93 sb.append(" }"); 94 sb.append("</script>"); 95 96 return sb.toString(); 97 98 } 99 }
x.tld文件
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <uri>/XuFanQi</uri> <tag> <name>Page</name> <tag-class>com.JavaGeneralPaging.tag.PageTag</tag-class> <body-content>JSP</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
jsp页面调用展示
导入标签
设置hidden隐藏域,放置page分页页码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <%@ taglib uri="/XuFanQi" prefix="x" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <h2>小说目录</h2> 13 <br> 14 15 <form action="${pageContext.request.contextPath}/bookServlet.action" 16 method="post"> 17 书名:<input type="text" name="bname"> <input type="submit" 18 value="确定"> 19 <input type="hidden" name="bname"> 20 </form> 21 <table border="1" width="100%"> 22 <tr> 23 <td>编号</td> 24 <td>名称</td> 25 <td>价格</td> 26 </tr> 27 <c:forEach items="${bookList }" var="b"> 28 <tr> 29 <td>${b.bid }</td> 30 <td>${b.bname }</td> 31 <td>${b.price }</td> 32 </tr> 33 </c:forEach> 34 </table> 35 36 <x:Page pageBean="${pageBean }"></x:Page> 37 38 </body> 39 </html>
测试结果:
搜索框:斗破
尾页
上一页
下一页
跳转页