通用分页 之 分页标签
1.导入资源
DBAccess.java config.properties资源文件 连接数据库
EncodingFiter.java 乱码处理
StringUtils.java 判断是否存在
Book .java 实体类
PageTag.java 自定义标签的助手类
BookDao.java dao方法
PageBean.java 分页的工具类
2.分析 分页的需求
页码,每页所要展示的数据,总记录数,是否分页,点击下一页或者上一页所需要的地址,存取分页时所需要的参数
分页的工具类
package com.zking.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
private String url;// 点击下一页或者上一页所需要的地址
private Map<String, String[]> paramMap = new HashMap<>();// 存取分页时所需要的参数
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));// 从servlet中接收page,再重载setPage
this.setRows(req.getParameter("rows"));
this.setPagination(req.getParameter("pagination"));
// getRequestURIL 获取到游览器的全路径
this.setUrl(req.getRequestURL().toString());
// getParameterMap 可以获取到一次URL请求所携带的所有参数
this.setParamMap(req.getParameterMap());
}
private void setPagination(String pagination) {
if (StringUtils.isNotBlank(pagination)) {
this.setPagination(!"false".equals(pagination));// 是否分页
}
}
private void setRows(String rows) {
if (StringUtils.isNotBlank(rows)) {
this.setRows(Integer.valueOf(rows));// 设置rows的值,每页展示多少数据
}
}
private void setPage(String page) {
if (StringUtils.isNotBlank(page)) {
this.setPage(Integer.valueOf(page));// 设置page的值,展示的页数
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String[]> paramMap) {
this.paramMap = paramMap;
}
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
/**
* 获取总页数
*
* @return
*/
public int getMaxPage() {
return this.total % this.rows == 0 ? this.total % this.rows : this.total / this.rows + 1;
}
/**
* 获取下一页页码
*
* @return
*/
public int getNextPage() {
return this.page < this.getMaxPage() ? this.page + 1 : this.page;
}
/**
* 获取上一页页码
*
* @return
*/
public int getPreviousPage() {
return this.page > 1 ? this.page - 1 : this.page;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
3.前台的标签
我们不能直接运行这个界面,这个界面需要点击查询之后才能有数据。因为我们没有传值
下面是我们的select界面的代码也是我们分页的桥梁
package com.zking.web;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.BookDao;
import com.zking.entity.Book;
import com.zking.util.PageBean;
public class Bookservlet extends HttpServlet {
private BookDao bookdao = new BookDao();//私有化dao方法,外部不能被访问
private static final long serialVersionUID = 4806795866251349977L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String bname = req.getParameter("bname");//接收bname,查询的关键字
Book book = new Book();
book.setBname(bname);
PageBean pageBean = new PageBean();
pageBean.setRequest(req);//将一些数据存到pageBean里
try {
List<Book> list = bookdao.list(book, pageBean);//查询出符合条件的某一页的记录数
req.setAttribute("bookList", list);//存值
req.setAttribute("pagebean", pageBean);
req.getRequestDispatcher("bookList.jsp").forward(req, resp);//转发
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
z标签,也就是我们的分页标签还有一个主要的类-----助手类,整个分页的灵魂所在。
package com.zking.tag;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.zking.util.PageBean;
/**
* 自定义标签 分页
* @author huguiyun
*
*/
public class PageTag extends BodyTagSupport {
private static final long serialVersionUID = -3609493782238830873L;
private PageBean pageBean;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
//下一次请求提交到后台的表单html代码拼接
sb.append("<form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>");
Map<String, String[]> paramMap = pageBean.getParamMap();
if(paramMap!=null && paramMap.size()>0) {//查询后的分页,传参
for (Entry<String,String[]> entry : paramMap.entrySet()) {
if(!"page".equals(entry.getKey())) {
for(String val : entry.getValue()) {
sb.append("<input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>");
}
}
}
}
sb.append("<input type='hidden' name='page'>");
sb.append("</form>");
//分页条html代码拼接
sb.append("<div style='text-align: right; font-size: 12px;'>");
sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a href='javascript:gotoPage(1)'>首页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getPreviousPage()+")'>上一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a> <input type='text'");
sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'> <a");
sb.append(" href='javascript:skipPage()'>Go</a>");
sb.append("</div>");
//分页所需调用的js代码
sb.append("<script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append("</script>");
return sb.toString();
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
}
没有模糊查的分页结果
有模糊查的分页结果