1,准备工作(导入jar包)
2,类结构(建包建类)
3,util里的各个类,各类有不同的作用
连接数据库—DBAccess
package util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBAccess {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBAccess.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.cj.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBAccess.getConnection();
System.out.println(conn);
DBAccess.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
中文乱码处理—EncodingFilter
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.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();
}
}
}
分页工具类—PageBean
package 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;//保存上一次的请求的URL
private Map<String, String[]> paramMap = new HashMap<>();//保存上一次的请求参数
/*
* 初始化pagebean,保存上一次的重要参数
*/
public void setRequest(HttpServletRequest req) {
// 1.需要保存上一次请求的URL
this.setUrl(req.getRequestURI().toString());
// 2.需要保存上一次请求的参数 bname
this.setParamMap(req.getParameterMap());
// 3.需要保存上一次的请求分页设置 pagination
this.setPagination(req.getParameter("pagination"));
// 4.需要保存上一次请求的展示条目数
this.setRows(req.getParameter("rows"));
// 5.初始化请求的页码 page
this.setPage(req.getParameter("page"));
}
private void setPage(String page) {
if(StringUtils.isNotBlank(page))
this.setPage(Integer.valueOf(page));
}
private void setRows(String rows) {
if(StringUtils.isNotBlank(rows))
this.setRows(Integer.valueOf(rows));
}
private void setPagination(String pagination) {
if(StringUtils.isNotBlank(pagination))
this.setPagination(!"false".equals(pagination));
}
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;
}
/*
* 最大页
*/
public int maxPage() {
return this.total % this.rows ==0 ? this.total / this.rows : this.total / this.rows + 1;
}
/*
* 下一页
*/
public int nextPage() {
return this.page < this.maxPage() ? this.page + 1 : this.page;
}
/*
* 上一页
*/
public int previousPage() {
return this.page > 1 ? this.page - 1 : this.page;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
public PageBean(int page, int rows, int total, boolean pagination, String url, Map<String, String[]> paramMap) {
super();
this.page = page;
this.rows = rows;
this.total = total;
this.pagination = pagination;
this.url = url;
this.paramMap = paramMap;
}
}
4、私有的构造方法——StringUtils
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);
}
}
config.properties 连接数据库文件
#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ora9
#user=test
#pwd=test
#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1423;DatabaseName=test
#user=sa
#pwd=sa
#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888
#mysql8
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
user=root
pwd=123456
一,通用后台分页(反射通用后台分页/查询方法)
BookDao类
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import entity.Book;
import util.DBAccess;
import util.PageBean;
import util.StringUtils;
/**
简单的查询方法
思路:
1,建立数据库连接 2,预定义PrepareStatement
3,查询结果集 4,处理结果集
分析不足之处 (假如换一张表t_jsoup_article)
1,以上3步(建立数据库连接 预定义PrepareStatement) 查询结果集 都是需要重复的,代码重复的
2,都是要返回数据库表对应的集合(集合中的对象都是不同的)
3,都是要处理结果集 rs(代码不一样,不同表会有不同的字段,那么实体类对应的属性就会不一样)
总结:需要写大量的重复代码,系统中的查询功能越多,重复代码量越大
解决方法:
1,抽取变化部分作为参数(sql,class对象)
2,抽取公共的父类,把重复代码剥离出来
*/
public class BookDao extends BaseDao<Book>{
public List<Book> list1(Book book) throws Exception{
List<Book> books=new ArrayList<Book>();
String sql="select * from t_mvc_book where 1=1 ";
String bname = book.getBname();//书籍名字
//书籍名字模糊查询 名字是从jsp页面传递过来的
if(StringUtils.isNotBlank(bname)) {
sql += "and bname like '%"+bname+"%'";
}
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
books.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
}
return books;
}
public List<Book> list2(Book book) throws Exception{
String sql="select * from t_mvc_book where 1=1 ";
String bname = book.getBname();//书籍名字
//书籍名字模糊查询 名字是从jsp页面传递过来的
if(StringUtils.isNotBlank(bname)) {
sql += "and bname like '%"+bname+"%'";
}
return super.list(sql, Book.class);
}
public List<Book> list3(Book book,PageBean pageBean) throws Exception{
String sql="select * from t_mvc_book where 1=1 ";
String bname = book.getBname();//书籍名字
//书籍名字模糊查询 名字是从jsp页面传递过来的
if(StringUtils.isNotBlank(bname)) {
sql += "and bname like '%"+bname+"%'";
}
return super.executeQuery(sql, Book.class,pageBean);
}
/*
* 思考:
* 在项目中大量的出现分页要求
* 目标:
* 想要做一个通用的分页查询,子类去继承父类,自带了分页功能,并且代码量极少;
* 实现:
* 1.mysql分页(分页通常有关键的因数:第page页,显示数目(rows),符合查询条件的总记录数(total))
* sql=SELECT * FROM t_mvc_book wHERE bname like '%圣墟%'
* pagesql= limit (n-1)*rows , rows
* countsql=select count(1) from (sql) t
* 2.编码思路:
* 需要返回的页数数据,要把SQL加工成pageSQL
* 需要分页,要把SQL加工成countSQL
* 共N页的算法:total % rows == 0 ?total /rows : total / tows +1;
*/
public static void main(String[] args) throws Exception {
BookDao bookDao = new BookDao();
Book book = new Book();
book.setBname("圣墟");
// List<Book> list=bookDao.list1(book);
// List<Book> list=bookDao.list2(book);
PageBean pageBean = new PageBean();
// pageBean.setPage(2); 查询第二页
// pageBean.setPagination(false); 不分页
List<Book> list=bookDao.list3(book,pageBean);
for (Book b : list) {
System.out.println(b);
}
}
}
缺点总结:需要写大量重复代码,系统中的查询功能越来越多,重复代码量变大
解决方案:
1、抽取变化部分作为参数:sql、Class
2、抽取公共的父类,把重复的代码剥离出来
baseDao类
package dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import entity.Book;
import util.DBAccess;
import util.PageBean;
import util.StringUtils;
/**
解决方法:
1,抽取变化部分作为参数(sql,class对象)
2,抽取公共的父类,把重复代码剥离出来
*/
public class BaseDao<T> {
public List<T> list(String sql,Class<T> clz) throws Exception{
List<T> list=new ArrayList<T>();
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
T t = clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
return list;
}
/**
* 通用分页查询
*/
public List<T> executeQuery(String sql,Class<T> clz,PageBean pageBean) throws Exception{
List<T> list=new ArrayList<T>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
/*
* 是否需要分页?
* 无需分页(项目中的下拉框)
* 必须分页(列表需求)
*/
if(pageBean != null && pageBean.isPagination()) {
// 必须分页(列表需求)
String countSQL = getPagesSQL(sql);
pst = con.prepareStatement(countSQL);//符合条件的某一页数据
rs = pst.executeQuery();
if(rs.next()) {
pageBean.setTotal(String.valueOf(rs.getObject(1)));
}
String pageSQL = getPagesSQL(sql,pageBean);
pst = con.prepareStatement(pageSQL);//符合条件的某一页数据
rs = pst.executeQuery();
}else {
// 无需分页(项目中的下拉框)
pst = con.prepareStatement(sql);//符合条件的所有数据
rs = pst.executeQuery();
}
while(rs.next()) {
T t = clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
return list;
}
/*
* 将原生SQL转换成countSQL
*/
private String getPagesSQL(String sql) {
return "select count(1) from ("+sql+") t";
}
/*
* 将原生SQL转换成pageSQL
*/
private String getPagesSQL(String sql,PageBean pageBean) {
return sql +" limit "+ pageBean.getStartIndex()+","+pageBean.getRows();
}
}
测试:
public static void main(String[] args) throws Exception {
BookDao bookDao = new BookDao();
Book book = new Book();
book.setBname("圣墟");
// List<Book> list=bookDao.list1(book);
// List<Book> list=bookDao.list2(book);
PageBean pageBean = new PageBean();
// pageBean.setPage(2); 查询第二页
// pageBean.setPagination(false); 不分页
List<Book> list=bookDao.list3(book,pageBean);
for (Book b : list) {
System.out.println(b);
}
}
所用的实体类:Book类
package entity;
public class Book {
private int bid;
private String bname;
private float price;
public Book(int bid, String bname, float price) {
super();
this.bid = bid;
this.bname = bname;
this.price = price;
}
public Book() {
// TODO Auto-generated constructor stub
}
@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;
}
}
案例:Acticle类
package entity;
public class Acticle {
private int id;
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Acticle [id=" + id + ", title=" + title + "]";
}
}
ActicleDao类
package dao;
import java.util.List;
import entity.Acticle;
import util.PageBean;
import util.StringUtils;
public class ActicleDao extends BaseDao<Acticle>{
public List<Acticle> list1(Acticle acticle) throws Exception{
return super.list("select * from t_jsoup_article ", Acticle.class);
}
/*
* 文章的分页查询 1-5页
*/
public List<Acticle> list2(Acticle acticle,PageBean pageBean) throws Exception{
String sql = "select * from t_jsoup_article where 1=1 ";
String title = acticle.getTitle();
if(StringUtils.isNotBlank(title)) {
sql += "and title like '%"+title+"%'";
}
return super.executeQuery(sql, Acticle.class,pageBean);
}
public static void main(String[] args) throws Exception {
ActicleDao acticleDao = new ActicleDao();
// List<Acticle> list = acticleDao.list1(null);
Acticle acticle = new Acticle();
PageBean pageBean = new PageBean();
pageBean.setRows(5);
acticle.setTitle("分析");
List<Acticle> list = acticleDao.list2(acticle,pageBean);
for (Acticle a : list) {
System.out.println(a);
}
}
}
测试:
public static void main(String[] args) throws Exception {
ActicleDao acticleDao = new ActicleDao();
// List<Acticle> list = acticleDao.list1(null);
Acticle acticle = new Acticle();
PageBean pageBean = new PageBean();
// pageBean.setRows(5); 一页展示5条
// acticle.setTitle("分析"); 模糊查询与分析相关的
List<Acticle> list = acticleDao.list2(acticle,pageBean);
for (Acticle a : list) {
System.out.println(a);
}
}
二、通用前台分页
1,目标
想要做一个通用的分页查询,子类去继承父类,自带分页功能,并且代码量极少
最终当碰到分页的需求的时候,只要写少量代码
2,MySql分页
分页通常有关键因素:第page页、显示数目(rows)、符合查询条件的总记录数(total)
原生sql=select * from t_mvc_book where bname like '%圣墟%'
pageSql=sql limit (n-1)*rows,rows
countSql=select count(1) from (sql) t
3,分页的核心思想
pageTag类
package 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 util.PageBean;
public class pageTag extends BodyTagSupport{
private PageBean pageBean; //包含所有分页相关的元素
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
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表单 这个就是上一次请求下一次重新发的奥义所在
// 上次请求的URL
sb.append("<form action='"+pageBean.getUrl()+"' id='pageBeanForm' method='post'>");
sb.append(" <input type='hidden' name='page'>");
// 上次请求的参数
Map<String, String[]> paramMap = pageBean.getParamMap();
if(paramMap != null && paramMap.size() > 0) {
Set<Entry<String, String[]>> entrySet = paramMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
// 参数名
String key = entry.getKey();
// 参数值
for ( String value : entry.getValue()) {
// 上一次请求的参数,再一次组装成了新的form表单
// 注意:page参数每次都会提交,我们需要避免
if("page".equals(key)) {
sb.append(" <input type='hidden' name='"+key+"' value='"+value+"'>");
}
}
}
}
sb.append(" </form>");
// 分页条
sb.append("<ul class='pagination justify-content-center'>");
sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+"'><a class='page-link'");
sb.append(" href='javascript:gotoPage(1)'>首页</a></li>");
sb.append(" <li class='page-item "+(pageBean.getPage() == 1 ? "disabled" : "")+" '><a class='page-link'");
sb.append(" href='javascript:gotoPage("+pageBean.previousPage()+")'><</a></li>");
// sb.append(" <li class=\'page-item\'><a class=\'page-link\' href=\'#\'>1</a></li>");
// sb.append(" <li class=\'page-item\'><a class=\'page-link\' href=\'#\'>2</a></li>");
sb.append(" <li class='page-item active'><a class='page-link' href='#'>"+pageBean.getPage()+"</a></li>");
sb.append(" <li class='page-item "+(pageBean.getPage() == pageBean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pageBean.nextPage()+")'>></a></li>");
sb.append(" <li class='page-item "+(pageBean.getPage() == pageBean.maxPage() ? "disabled" : "")+"'><a class='page-link' href='javascript:gotoPage("+pageBean.maxPage()+")'>尾页</a></li>");
sb.append(" <li class='page-item go-input'><b>到第</b><input class='page-link'");
sb.append(" type='text' id='skipPage' name=''><b>页</b></li>");
sb.append(" <li class='page-item go'><a class='page-link'");
sb.append(" href='javascript:skipPage()'>确定</a></li>");
sb.append(" <li class='page-item'><b>共"+pageBean.getTotal()+"条</b></li>");
sb.append(" </ul>");
// 分页执行的JS代码
sb.append("<script type='text/javascript'>");
sb.append(" <script type='text/javascript'>");
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.maxPage()+") {");
sb.append(" alert('请输入1~"+pageBean.getTotal()+"的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append(" </script>");
return sb.toString();
}
}
bookServlet类
package 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 dao.BookDao;
import entity.Book;
import util.PageBean;
@WebServlet("/book")
public class bookServlet 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 {
/*
* 目标:
* 每次做分页做分页需求的时候,都会有大量的重复的后台代码以及前台代码(后台代码以解决)
* 前台重复代码问题:
* 大量的前台HTML重复
* 大量的前台javaScript代码重复
* 目标:
* 定义<z:page><z:page>标签,无需任何HTML以及js,就可以完成前端分页
* 分析:
* 1.前台分页,上一页下一页相较于只不过是页码变了
* 1.需要保存上一次请求的URL
* 2.需要保存上一次请求的参数 bname
* 3.需要保存上一次的请求分页设置 pagination
* 4.需要保存上一次请求的展示条目数
* 5.初始化请求的页码 page
*
* 2.自定义jsp标签(page标签)
* 定义pagebean,因为pagebean中包含了分页中的所有元素(page/rows/pageination/total/nextPage/previoupage/maxpage)
*
* 前台传递下拉框: String[] hobbys =req.getParameterValues("");
*
* 接收jsp页面传递到后台的参数键值对,也就是说把parameterMap遍历,可以拿到我们要的参数(String[]可以接收单个参数,也可以接收数组)
* req.getParameterMap();
*
*/
BookDao bookDao = new BookDao();
Book book = new Book();
book.setBname(req.getParameter("bname"));
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list3 = bookDao.list3(book, pageBean);
req.setAttribute("book1", list3);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
tld文件
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>zking 1.1 core library</description>
<display-name>zking core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>zking</short-name>
<uri>http://jsp/jstl/core</uri>
<tag>
<name>page</name>
<tag-class>tag.pageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签的成员变量 -->
<name>pageBean</name>
<!-- 改成员变量是否必传 -->
<required>true</required>
<!-- 是否支持EL表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp/jstl/core" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
rel="stylesheet">
<script
src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍列表</title>
<style type="text/css">
.page-item input {
padding: 0;
width: 40px;
height: 100%;
text-align: center;
margin: 0 6px;
}
.page-item input, .page-item b {
line-height: 38px;
float: left;
font-weight: 400;
}
.page-item.go-input {
margin: 0 10px;
}
</style>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/book" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="bname"
placeholder="请输入书籍名称">
<!-- <input name="rows" value="20" type="hidden"> 一页显示二十行-->
<!-- <input name="pagination" value="false" type="hidden"> 不分页-->
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
</form>
<table class="table table-striped bg-success">
<thead>
<tr>
<th scope="col">书籍ID</th>
<th scope="col">书籍名</th>
<th scope="col">价格</th>
</tr>
</thead>
<tbody>
<c:forEach var="b" items="${book1 }">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
</tr>
</c:forEach>
</tbody>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>