目录
一、改造PageBean
二、分页查询
一、改造PageBean
改造PageBean条件:
需要新增变量保存上一次查询条件
需要新增变量保存上一次请求地址
需要添加方法:获取最大页的页码
需要添加方法:获取上一页的页码
需要添加方法:获取下一页的页码
需要新增方法:初始化pageBean
PageBean类:
package com.zwc.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 parameterMap = new HashMap();
// 需要添加方法:获取最大页的页码
public int maxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total /this.rows +1;
}
// 需要添加方法:获取上一页的页码
public int perviousPage() {
return this.page>1?this.page-1 : this.page;
}
// 需要添加方法:获取下一页的页码
public int nextPage() {
//System.out.println(page);
return this.page < this.maxPage() ? this.page+1 : this.page;
}
// 需要新增方法:初始化pageBean
public void setRequest(HttpServletRequest request) {
this.setPage(request.getParameter("page"));
this.setRows(request.getParameter("rows"));
this.setPagination(request.getParameter("Pagination"));
this.setUrl(request.getRequestURL().toString());
this.setParameterMap(request.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));
}
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page))
// set自动生成的方法
this.setPage(Integer.valueOf(page));
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getParameterMap() {
return parameterMap;
}
public void setParameterMap(Map parameterMap) {
this.parameterMap = parameterMap;
}
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
+ ", url=" + url + ", parameterMap=" + parameterMap + "]";
}
}
ServletPage类:
package com.zwc.web;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zwc.entity.Book;
import com.zwc.util.PageBean;
import com.zwcdao.BookDao;
@WebServlet("/book/search")
public class BookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
}
}
需要新增变量保存上一次查询条件
需要新增变量保存上一次请求地址
需要添加方法:获取最大页的页码
需要添加方法:获取上一页的页码
需要添加方法:获取下一页的页码
需要新增方法:初始化pageBean
PageBean类:
package com.zwc.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 parameterMap = new HashMap();
// 需要添加方法:获取最大页的页码
public int maxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total /this.rows +1;
}
// 需要添加方法:获取上一页的页码
public int perviousPage() {
return this.page>1?this.page-1 : this.page;
}
// 需要添加方法:获取下一页的页码
public int nextPage() {
//System.out.println(page);
return this.page < this.maxPage() ? this.page+1 : this.page;
}
// 需要新增方法:初始化pageBean
public void setRequest(HttpServletRequest request) {
this.setPage(request.getParameter("page"));
this.setRows(request.getParameter("rows"));
this.setPagination(request.getParameter("Pagination"));
this.setUrl(request.getRequestURL().toString());
this.setParameterMap(request.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));
}
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page))
// set自动生成的方法
this.setPage(Integer.valueOf(page));
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getParameterMap() {
return parameterMap;
}
public void setParameterMap(Map parameterMap) {
this.parameterMap = parameterMap;
}
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
+ ", url=" + url + ", parameterMap=" + parameterMap + "]";
}
}
ServletPage类:
package com.zwc.web;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zwc.entity.Book;
import com.zwc.util.PageBean;
import com.zwcdao.BookDao;
@WebServlet("/book/search")
public class BookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
}
}
package com.zwc.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 parameterMap = new HashMap();
// 需要添加方法:获取最大页的页码
public int maxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total /this.rows +1;
}
// 需要添加方法:获取上一页的页码
public int perviousPage() {
return this.page>1?this.page-1 : this.page;
}
// 需要添加方法:获取下一页的页码
public int nextPage() {
//System.out.println(page);
return this.page < this.maxPage() ? this.page+1 : this.page;
}
// 需要新增方法:初始化pageBean
public void setRequest(HttpServletRequest request) {
this.setPage(request.getParameter("page"));
this.setRows(request.getParameter("rows"));
this.setPagination(request.getParameter("Pagination"));
this.setUrl(request.getRequestURL().toString());
this.setParameterMap(request.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));
}
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page))
// set自动生成的方法
this.setPage(Integer.valueOf(page));
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getParameterMap() {
return parameterMap;
}
public void setParameterMap(Map parameterMap) {
this.parameterMap = parameterMap;
}
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
+ ", url=" + url + ", parameterMap=" + parameterMap + "]";
}
}
ServletPage类:
package com.zwc.web;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zwc.entity.Book;
import com.zwc.util.PageBean;
import com.zwcdao.BookDao;
@WebServlet("/book/search")
public class BookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
}
}
BookList.jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="z" uri="http://jsp.zwcedu.cn" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
书籍列表
${pageBean}
运行结果
二、分页查询
书籍表为案例
BookList.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="z" uri="http://jsp.zwcedu.cn" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>书籍列表
| 书籍ID | 书籍名 | 价格 |
|---|---|---|
| 1 | 圣墟第1章 | 1 |
| 1 | 圣墟第1章 | 1 |
- 首页
- <
- 1
- 2
- 3
- >
- 尾页
- 到第页
- 确定
- 共666条
页面效果
在上面jsp代码中,可以看到以下
- 首页
- <
- 1
- 2
- 3
- >
- 尾页
- 到第页
- 确定
- 共666条
在这里我们可以把它变成标签,首先建立tld标签文件、在建立助手类
助手类
Pagetag类:
package com.zwc.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.zwc.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) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
// 隐藏的form表单,作用保存在上一次查询条件
sb.append("
");
// 分页条
sb.append("Pagetag类:
package com.zwc.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.zwc.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) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
// 隐藏的form表单,作用保存在上一次查询条件
sb.append("
- ");
sb.append("
- 首页 "); sb.append("
- < "); sb.append("
- "+pageBean.getPage()+" "); sb.append("
- > "); sb.append("
- 尾页 "); sb.append("
- 到第页 "); sb.append("
- 确定 "); sb.append("
- 共"+pageBean.getTotal()+"条 "); sb.append("
tld文件
JSTL 1.1 core library
JSTL core
1.1
z
http://jsp.zwcedu.cn
Provides core validation features for JSTL tags.
org.apache.taglibs.standard.tlv.JstlCoreTLV
page
com.zwc.tag.PageTag
JSP
pageBean
true
true
在jsp页面导入标签
接着BookServlet
package com.zwc.web;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zwc.entity.Book;
import com.zwc.util.PageBean;
import com.zwcdao.BookDao;
@WebServlet("/book/search")
public class BookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
BookDao bookDao = new BookDao();
Book book = new Book();
book.setBname(request.getParameter("bname"));
try {
List books = bookDao.list2(book, pageBean);
request.setAttribute("books", books);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
}
}
在到jsp页面用C:foreach标签遍历拿到数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="z" uri="http://jsp.zwcedu.cn" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
书籍列表
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="z" uri="http://jsp.zwcedu.cn" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>书籍列表
| 书籍ID | 书籍名 | 价格 |
|---|---|---|
| ${b.bid} | ${b.bname} | ${b.price} |
页面效果:
如果在你搜索乱码时
在EncodingFiter.java
package com.zwc.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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/book/*")
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();
}
}
}
package com.zwc.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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/book/*")
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();
}
}
}
接下来进行搜索可以看到没有乱码



