.将上一次查询请求再发一次,只不过改变了页码。
1、改造上次PageBean里 的内容;
2、页面展示
3、创建控制层 servlet
4、自定义分页标签
5、助手类信息
1. PageBean
分页三要素
page 页码 视图层传递过来
rows 页大小 视图层传递过来
total 总记录数 后台查出来
pagination 是否分页 视图层传递过来
2. 后台
2.1 entity
2.2 dao
第一次查满足条件的总记录数
第二次查指定页码并满足条件的记录
二次查询的条件要一致
2.3 控制层
Servlet
3. 视图层
PageTag
如何将上一次查询请求再发一次
String contextPath = req.getContextPath();//根目录
String url = req.getServletPath();//请求的地址
req.getRequestURL() //获取请求全路径
Map
package com.lww.util; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServlet; 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 MappaMap = new HashMap<>(); private String url; public void setRequest(HttpServletRequest req) { Map parameterMap = req.getParameterMap(); StringBuffer url = req.getRequestURL(); //保存上一次请求所携带的参数 this.setPaMap(req.getParameterMap()); this.setUrl(req.getRequestURL().toString()); //在jsp页面来控制是否分页 // this.setPagination(req.getParameter("pagination")); //在jsp页面控制一页展示多少条信息 // this.setRows(req.getParameter("rows")); this.setPage(req.getParameter("page")); } private void setPage(String page) { this.page = StringUtils.isNotBlank(page) ? Integer.valueOf(page):this.page; } public void setPagination(String pagination) { this.pagination = StringUtils.isNotBlank(pagination) ? !"false".equals(pagination):this.pagination; } public void setRows(String rows) { this.rows = StringUtils.isNotBlank(rows)?Integer.valueOf(rows):this.rows; } public Map getPaMap() { return paMap; } public void setPaMap(Map paMap) { this.paMap = paMap; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } 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; } /** * 获得起始记录的下标 */ public int getStartIndex() { return (this.page - 1) * this.rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } /** * 获取最大页码数 */ public int getMaxPage() { return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows+1; } /** * 下一页 */ public int getNexPage() { return this.page < this.getMaxPage() ? this.page+1 : this.page; } /** * 上一页 */ public int getPreviousPage() { return this.page > 1 ? this.page-1 : this.page; } }
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%@taglib prefix="z" uri="/lww" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
编号 | 名称 | 价格 |
${s.bid } | ${s.bname } | ${s.price} |
package com.lww.web;
import java.io.IOException;
import java.sql.SQLException;
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 dao.BookDao;
import entity.Book;
import util.PageBean;
public class BookServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
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 bean = new PageBean();
try {
bean.setRequest(req);
List
req.setAttribute("bookList", list);
req.setAttribute("pageBean", bean);
req.getRequestDispatcher("/BookList.jsp").forward(req, resp);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.lww.web;
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 util.PageBean;
public class PageTag extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
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.print(toHTML());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuilder sb= new StringBuilder();
sb.append("
//无脑操作(复制粘贴即可)
sb.append("");
return sb.toString();
}
}(注意在拼接时需要注意空格)
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
package util;
public class StringUtils {
// 私有的构造方法,保护此类不能在外部实例化
private StringUtils() {
}
/**
* 如果字符串等于null或去空格后等于"",则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isBlank(String s) {
boolean b = false;
if (null == s || s.trim().equals("")) {
b = true;
}
return b;
}
/**
* 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isNotBlank(String s) {
return !isBlank(s);
}
}
package util;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 中文乱码处理
*
*/
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();
}
}
}
目录:
下一页:
上一页:
尾页:
跳转: