通用分页(二)

通用分页二

接着昨天的来
完整的pageBean
jsp界面
PageTag

action包

package com.tang.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tang.biz.ICompanyBiz;
import com.tang.biz.impl.CompanyBiz;
import com.tang.entity.Company;
import com.tang.util.PageBean;

public class CompanyServlet extends HttpServlet {
	
	@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 {
		//1.设置编码
		req.setCharacterEncoding("utf-8");
		
		HttpSession session = req.getSession();
		//2.获取页面传过来的值 
		String job = req.getParameter("job");
		
		System.out.println(req.getContextPath());
		
		System.out.println(req.getServletPath());
		
		System.out.println(req.getRequestURI());
		
		//Map parameterMap = req.getParameterMap();
		PageBean pageBean = new PageBean();
		pageBean.setRequest(req);
		Company company = new Company();
		company.setJob(job);
		//3.调用biz层查询
		ICompanyBiz icb = new CompanyBiz();
		
		List listCompany = icb.listCompany(company, pageBean);
//		try {
//			listBook = bookBiz.listBook(book,pageBean);
//		} catch (Exception e) {
//			throw new RuntimeException();
//		}
		
		//4.将查询出的集合放到session作用域中
		session.setAttribute("listCompany", listCompany);
		session.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("listCompany.jsp").forward(req, resp);
	}
	
}

把CompanyServlet类,还有EncodingFilter类进行配置,不然运行出错
web.xml



  J2EE_paging
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  
  	EncodingFilter
  	com.tang.util.EncodingFiter
  
  
  	EncodingFilter
  	/*
  
  
  
  	CompanyServlet
  	com.tang.action.CompanyServlet
  
  
  	CompanyServlet
  	/listCompany.action
  
  

pageBean

package com.tang.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class PageBean {
		
private Integer page =1; //页码
	
	private Integer rows =10; //页大小
	
	private Integer total=0;  //总记录数
	
	private boolean pagination=true;  //默认 false不分页
	
	private String url;//请求地址
	
	private Map map;//请求参数集合

	public PageBean() {
		super();
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	
	public void setPage(String page) {
		if(page!=null && !"".equals(page)) {
			this.page = Integer.parseInt(page);
		}
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	public void setRows(String rows) {
		if(rows!=null && !"".equals(rows)) {
			this.rows = Integer.parseInt(rows);
		}
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer 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;
	}
	
	public void setPagination(String pagination) {
		if(pagination!=null && !"".equals(pagination)) {
			this.pagination = Boolean.parseBoolean(pagination);
		}
	}

	public String getUrl() {
		return url;
	}

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

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}	
	
	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
	}

	public int getStartIndex() {
		return (this.page-1)*this.rows;
	}
	
	/**
	 * 对pageBean初始化
	 * @param req
	 */
	public void setRequest(HttpServletRequest req) {
		String page = req.getParameter("page");//页码
		String rows = req.getParameter("rows");//页大小
		String pagination = req.getParameter("pagination");//是否分页
		
		this.setPage(page);
		this.setRows(rows);
		this.setPagination(pagination);
		this.setUrl(req.getRequestURI());
		this.setMap(req.getParameterMap());
		
	}
	
	/**
	 * 得到上一页的页码
	 * @return
	 */
	public int getPerPage() {
		int perPage = this.page-1;
		if(perPage<1) {
			perPage = 1;
		}
		return perPage;
	}
	
	/**
	 * 得到下一页的页码
	 * @return
	 */
	public int getNextPage() {
		int nextPage = this.page+1;
		if(nextPage>getMaxPage()) {
			nextPage = getMaxPage();
		}
		return nextPage;
	}
	
	/**
	 * 得到最大的页码
	 * @return
	 */
	public int getMaxPage() {
		int maxPage = this.total / this.rows;
		if(this.total % this.rows != 0) {
			maxPage ++;
		}
		return maxPage;
	}
	
}

pageTag(是把jsp界面中的一些代码包装起来,整理在一起)

package com.tang.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.tang.util.PageBean;

public class PageTag extends BodyTagSupport {
	private PageBean pageBean;

	public PageTag() {
		super();
	}

	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.println(toHtml());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return super.doStartTag();
	}
	
	
	private String toHtml() {
		//pageBean为空或者 不分页
		if(null==pageBean || !pageBean.isPagination()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		
		//拼接form表单
		/*
*/ sb.append("
"); sb.append(""); //参数列表 Map map = pageBean.getMap(); String name =null; String[] values = null; Set> entrySet = map.entrySet(); for (Entry entry : entrySet) { name = entry.getKey(); if(name.equals("page")) { continue; } values = entry.getValue(); for (String value : values) { sb.append(""); } } sb.append(""); //拼接div部分 /*
第几页/总共多少页,共多少条记录 首页  上一页  下一页  末页    GO
*/ sb.append("
"); sb.append("第"+pageBean.getPage()+"页/总共"+pageBean.getMaxPage()+"页,共"+pageBean.getTotal()+"条记录 "); //首页 上一页 if(pageBean.getPage()!=1) { sb.append("首页 \r\n" + " 上一页 "); }else { sb.append("首页 上一页 "); } //末页 下一页 if(pageBean.getPage()!=pageBean.getMaxPage()) { sb.append("下一页 \r\n" + " 末页 "); }else { sb.append("下一页 末页 "); } //跳转页 sb.append(" "); sb.append("GO"); sb.append("
"); //拼接js /**/ sb.append(""); return sb.toString(); } }

在写一个自定义标签,把pageTag配置到z.tld中




	
	1.0
	
	1.2
	
	l
	
	/zking

	
		
		page
		
		com.tang.tag.PageTag
		
		empty
		
		
			
			pageBean
			
			true
			
			true
		
	

jsp界面

<%@ 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" %>






公司信息



		

公司信息

职位:
序号 职位 公司 工作地址 薪资 学历要求 爬取时间 常有详情
${s.count } ${c.job } ${c.company } ${c.address } ${c.salary } ${c.limit } ${c.time } 常有详情
<%--
第${pageBean.getPage()}页/总共${pageBean.getMaxPage()}页 一共${pageBean.getTotal()}条记录 首页  上一页  下一页  末页    GO 
--%>

界面
查询所有:
通用分页(二)_第1张图片
模糊查询,查询工程,查出有两页,13条记录:
第一页:
通用分页(二)_第2张图片
第二页:
通用分页(二)_第3张图片

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