通用分页[二]

通用分页[二]

通用分页核心思路
将上一次查询请求再发一次,只不过页码变了

思路:

  1. 对pagebean增强(添加url、paramMap属性,添加初始化pagebean的方法、最大页、上一页、下一页方法)
  2. 自定义page分页标签(助手类、tld、jsp引用标签库)
  3. 将按钮条写入标签助手类
  4. 将隐藏的form表单写入标签助手类
  5. 将对应的js代码写入标签助手类
  6. 测试通用分页是否完善

我们定义一个实体类


public class Book {
	private int bid;
	private String bname;
	private float price;

	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getBname() {
		return bname;
	}

	public void setBname(String bname) {
		this.bname = bname;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public Book(int bid, String bname, float price) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.price = price;
	}

	public Book() {
		super();
	}
}


public class PageTag extends BodyTagSupport{
	private PageBean pageBean;

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.write(toHTML());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return super.doStartTag();
	}

	public String toHTML() {
		StringBuffer sb = new StringBuffer();
		//下一次请求提交到后台的表单html代码拼接
		sb.append("
"); Map paramMap = pageBean.getParamMap(); if (paramMap != null && paramMap.size() > 0) { for (Entry entry : paramMap.entrySet()) { if (!"page".equals(entry.getKey())) { for(String val : entry.getValue()) { sb.append(""); } } } } sb.append(""); sb.append(""); // 分页条html代码拼接 sb.append("
"); sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页  首页  上一页  下一页  尾页    Go"); sb.append("
"); // 分页所需要的js代码 sb.append(""); return sb.toString(); } }

/**
 * 分页工具类
 *
 */
public class PageBean {

	private int page = 1;// 页码

	private int rows = 10;// 页大小

	private int total = 0;// 总记录数

	private boolean pagination = true;// 是否分页
	
//	如何将查询关键字传到下一次请求?
//	   将查询参数保存到pagebean中
//	   Map paramMap 存储参数键值对
	private String url;
	private Map paramMap = new HashMap<>();
	
	public void setRequsest(HttpServletRequest req) {
		this.setPage(req.getParameter("page"));
		this.setRows(req.getParameter("rows"));
		this.setPagination(req.getParameter("pagination"));
		//req.getRequestURL()获取浏览器请求的全路径 
		this.setUrl(req.getRequestURL().toString());
		//req.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));
		}
	}

	public void setPage(String page) {
		if(StringUtils.isNotBlank(page)) {
			this.setPage(Integer.valueOf(page));
		}
	}
	//获取最大页数
	public int getMaxPage() {
		return this.total%this.rows==0?this.total/this.rows:(this.total/this.rows)+1;
	}
	//获取下一页页码
	public int getNextPage() {
		return this.page1?this.page-1:this.page;
	}
	
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map 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;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
	}

}




    
  zking 1.1 core library
  JSTL core
  1.1
  c
  /zking
  
  
  
    page
    
    com.tags.PageTag
    
    JSP
    
    	
        pageBean
        true
        true
    
  
  

这是servlet

public class BookServlet extends HttpServlet {
	private BookDao bookDao = new BookDao();
	
	@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");
		Book book = new Book();
		book.setBname(bname);
		PageBean pageBean = new PageBean();
		pageBean.setRequsest(req);
		try {
			//查询符合条件的某一页的数据
			List list = bookDao.list(book, pageBean);
			req.setAttribute("bookList", list);
			req.setAttribute("pagebean", pageBean);
			req.getRequestDispatcher("bookList.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

xml配置一下



  Pagebean
  
  	bookServlet
  	com.web.BookServlet
  
  
  	bookServlet
  	/bookServlet
  
  
  //这个是配置解决乱码的过滤器
  
  	encodingFiter
  	com.util.EncodingFiter
  
  
  	encodingFiter
  	/*
  


/**
 * 中文乱码处理
 * 
 */
public class EncodingFiter implements Filter {

	private String encoding = "UTF-8";// 默认字符集

	public EncodingFiter() {
		super();
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		// 中文处理必须放到 chain.doFilter(request, response)方法前面
		res.setContentType("text/html;charset=" + this.encoding);
		if (req.getMethod().equalsIgnoreCase("post")) {
			req.setCharacterEncoding(this.encoding);
		} else {
			Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合
			Set set = map.keySet();// 取出所有参数名
			Iterator it = set.iterator();
			while (it.hasNext()) {
				String name = (String) it.next();
				String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组]
				for (int i = 0; i < values.length; i++) {
					values[i] = new String(values[i].getBytes("ISO-8859-1"),
							this.encoding);
				}
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集
		if (null != s && !s.trim().equals("")) {
			this.encoding = s.trim();
		}
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	<%@ taglib prefix="z" uri="/zking" %>




Insert title here


小说目录


书名:
编号 名称 价格
${b.bid } ${b.bname } ${b.price }

效果:
通用分页[二]_第1张图片
点击下一页:通用分页[二]_第2张图片
点击尾页:通用分页[二]_第3张图片
点击首页:
通用分页[二]_第4张图片

你可能感兴趣的:(通用分页[二])