目录
一.重构-提取公用方法
1.为了进行公共方法的抽取,需要找出上面实习中的可通用部分,和差异化部分
2.公用方法封装思路
3. 具体实现
二.分页标签
2.1 准备一个Servlet
3.2 结果展示页面
三. 过滤器解决中文乱码问题
四.加入分页功能
四.封装分页标签
编写助手类
标签库描述文件中添加paging标签
使用分页标签首先在页面中引入标签
将原来的分页功能,替换为标签即可
MySQL分页
最后结果展示页面
一.重构-提取公用方法
1.为了进行公共方法的抽取,需要找出上面实习中的可通用部分,和差异化部分
- .只要是分页,就会统计总记录数,而总记录数的统计是在业务sql外封装了一个select count(*)是有规律可循的,可以通用
- 只要是分页,则封装分页sql也是有规律可循的(在业务sql后加limit子句即可),可以通用
- 因为每个查询对应的业务实体(即模型)不同,所以ORM映射部分不能通用
2.公用方法封装思路
-
将可通用的部分封装到模板中
-
差异化部分(即不可通用部分),可以定义一个处理接口,以便于通过参数传入个性化的实现部分
3. 具体实现
通用分页查询模板类:
public final class DBTemplate {private DBTemplate() {}public static interface IORMConvert<T> {List<T> convert(ResultSet rs) throws SQLException;}public static <T> List<T> query(String sql, Object[] params, PageBean pageBean, IORMConvert<T> convert) {List<T> datas = new ArrayList<>();Connection con = null;PreparedStatement ps = null;ResultSet rs = null;//不需要分页if (pageBean == null || !pageBean.isPagination()) {try {con = DBUtil.getConection();ps = con.prepareStatement(sql);setParam(params, ps);rs = ps.executeQuery();datas = convert.convert(rs);return datas;} catch(Exception e) {e.printStackTrace();} finally {DBUtil.closeDB(rs, ps, con);}} else {//1. 查询总记录数//2. 查询当前页数据//1. 生成统计总记录数的SQL, 查询总记录数try {String countSql = "select count(*) from (" + sql + ") tmp";con = DBUtil.getConection();ps = con.prepareStatement(countSql);setParam(params, ps);rs = ps.executeQuery();while(rs.next()) {pageBean.setTotal(rs.getInt(1));}/** 如果统计的总记录数为0,则表示没有符合条件的记录,直接返回一个空结果集即可。*/if(pageBean.getTotal() == 0) {return datas;}} catch (Exception e) {e.printStackTrace();} finally {if(pageBean.getTotal() == 0) {DBUtil.closeDB(rs, ps, con);}DBUtil.closeDB(rs, ps);}//查询当前页数据try {String pagingSql = sql + " limit " + pageBean.getStartRow() + ", " + pageBean.getRows();ps = con.prepareStatement(pagingSql);setParam(params, ps);rs = ps.executeQuery();datas = convert.convert(rs);} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.closeDB(rs, ps, con);}}return datas;}private static void setParam(Object[] params, PreparedStatement ps) throws SQLException {if (params != null) {int i = 1;for (Object param : params) {ps.setObject(i, param);i++;}}}}
使用示例:
public class StudentDao2 {public List<Student> getStudents(String sname, PageBean pageBean) {String sql = "select * from t_student where sname like ?";return DaoTemplate.query(sql, new Object[] {sname}, pageBean, new IORMConvert<Student>() {@Overridepublic List<Student> convert(ResultSet rs) throws SQLException {List<Student> stus = new ArrayList<>();while(rs.next()) {Student stu = new Student();stu.setSid(rs.getInt("sid"));stu.setSname(rs.getString("sname"));stu.setAge(rs.getInt("age"));stu.setRemark(rs.getString("remark"));stus.add(stu);}return stus;}});}public static void main(String[] args) {StudentDao2 dao = new StudentDao2();PageBean pageBean = new PageBean();pageBean.setPage(3);List<Student> students = dao.getStudents("张%", pageBean);students.forEach(s -> System.out.println(s));}}
二.分页标签
2.1 准备一个Servlet
准备一个servlet用于处理请求,获取数据库中的数据,并转发到结果显示页面
@WebServlet(value = "/students")
public class StudentAction extends HttpServlet {private static final long serialVersionUID = 3152900867611381148L;private StudentDao2 studentDao = new StudentDao2();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PageBean pageBean = new PageBean();pageBean.setRequest(request);request.setAttribute("pageBean", pageBean);String sname = request.getParameter("sname");List<Student> students = studentDao.getStudents(sname+"%", pageBean);request.setAttribute("students", students);System.out.println("dopost .......... ");request.getRequestDispatcher("/students/stuList.jsp").forward(request, response);}}
3.2 结果展示页面
创建一个页面,该页面用于显示结果, 使用jstl的c标签来展示结果,为正常使用c标签,需要引入jstl-1.2.jar和standard-1.1.2.jar。
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@taglib prefix="z" uri="/zking" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><h1>学生信息</h1><form action="<%=request.getContextPath()%>/students" method="post"><input type="text" name="sname"><input type="submit" value="查询"></form><table border="1" style="width: 98%;"><tr><td>学号</td><td>姓名</td><td>年龄</td><td>备注</td></tr><c:forEach items="${students}" var="student"><tr><td>${student.sid}</td><td>${student.sname}</td><td>${student.age}</td><td>${student.remark}</td></tr></c:forEach><z:paging pageBean="${pageBean}"/></table>
</body>
</html>
三. 过滤器解决中文乱码问题
/*** 中文乱码处理*/@WebFilter("/*")
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();}}}
四.加入分页功能
- 先不考虑功能性在页面上的table标签下,加入及分页工具条
<div style="text-align: right; width:98%;">第1页 共100条记录 <a>首页</a> <a>上页</a> <a>下页</a> <a>尾页</a> 第<input type="text" size="2" /> <a href="#">GO</a></div>
- 不考虑通过的分页如下实现,先在结果页面中实现分页功能。
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<%@taglib prefix="z" uri="/zking" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><h1>学生信息</h1><form action="<%=request.getContextPath()%>/students" method="post"><input type="text" name="sname"/><input type="submit" value="查询"></form><table border="1" style="width: 98%;"><tr><td>学号</td><td>姓名</td><td>年龄</td><td>备注</td></tr><c:forEach items="${students}" var="student"><tr><td>${student.sid}</td><td>${student.sname}</td><td>${student.age}</td><td>${student.remark}</td></tr></c:forEach></table><!-- 分页页面元素 --><div style="text-align: right; width:98%;">第${pageBean.page}页 共${pageBean.total}条记录 <a href="javascript: goPage(1);">首页</a> <a href="javascript: goPage('${pageBean.previousPage}');">上页</a> <a href="javascript: goPage('${pageBean.nextPage}');">下页</a> <a href="javascript: goPage('${pageBean.totalPage}')">尾页</a> 第<input type="text" id="pagingPageNum" size="2" onkeypress="goSpecifiedPage(event,this.value);"/> <a href="javascript: goPage(document.getElementById('pagingPageNum').value)">GO</a></div><!-- 用于分页的隐藏表单 --><form action="${pageBean.url}" id="pagingForm" method="post"><input type="hidden" name="page" value="${pageBean.page}"/><!-- 先只考虑本功能的查询参数,没有考虑公用性(不同功能的参数不同) --><input type="hidden" name="sname" value="<%=request.getParameter("sname")%>"/></form><!-- 用于分页的js代码 --><script>function goPage(pageNum) {var form = document.getElementById("pagingForm");form.page.value = pageNum;form.submit();}function goSpecifiedPage(event) {if(event.keyCode == 13) {var pageNum = document.getElementById("pagingPageNum").value;var form = document.getElementById("pagingForm");form.page.value = pageNum;form.submit();}}</script></body>
</html>
目前为止,分页功能已经实现了。
遗留下来的问题:
1 如果其他功能需要分页,则需要复制大量代码才能重用该功能
2 如果系统需要修改分页工具栏的显示风格呢?
四.封装分页标签
为了方便代码的复用,及可维护性,我们将分页功能封装了一个自定义标签(其实就是将原来写在页面中的代码,通过移入到自定义标签中去实现),开发自定义标签分成三步:
- 编写助手类
- 编写标签描述文件
- 在页面上引入标签库,并使用
编写助手类
public class PagingTag extends BodyTagSupport {private PageBean pageBean;public PageBean getPageBean() {return pageBean;}public void setPageBean(PageBean pageBean) {this.pageBean = pageBean;}@Overridepublic int doStartTag() throws JspException {JspWriter out = this.pageContext.getOut();try {out.println(buildHtml());return SKIP_BODY;} catch (IOException e) {throw new JspException("分页标签异常", e);}}//生成Html内容private String buildHtml() {//构建分页页面元素String pagingElement = "<div style=\"text-align: right; width:98%;\">\r\n" + " 第" + pageBean.getPage() + "页 \r\n" + " 共" + pageBean.getTotal() + "条记录 \r\n" + " <a href=\"javascript: goPage(1);\">首页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getPreviousPage() + "');\">上页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getNextPage() + "');\">下页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getTotalPage() + "')\">尾页</a> \r\n" + " 第<input type=\"text\" id=\"pagingPageNum\" size=\"2\" value='"+pageBean.getPage()+"' onkeypress=\"goSpecifiedPage(event,this.value);\"/> \r\n" + " <a href=\"javascript: goPage(document.getElementById('pagingPageNum').value)\">GO</a>\r\n" + " </div>";//构建隐藏表单,用于在分页时传递分页参数String hiddenForm = "<form action='" + pageBean.getUrl() + "' id=\"pagingForm\" method=\"post\">"+ "<input type=\"hidden\" name=\"page\" />";Map<String, String[]> parameterMap = pageBean.getParameterMap();for(Map.Entry<String, String[]> param: parameterMap.entrySet()) {String paramName = param.getKey();if("page".equals(paramName)) continue;String[] values = param.getValue();for(String val: values) {hiddenForm += "<input type='hidden' name='" + paramName + "' value='" + val + "'>";}}hiddenForm += "</form>";//构建分页功能需要的js代码块String script = "<script>\r\n" + " function goPage(pageNum) {\r\n" + " var form = document.getElementById(\"pagingForm\");\r\n" + " form.page.value = pageNum;\r\n" + " form.submit();\r\n" + " }\r\n" + " \r\n" + " function goSpecifiedPage(event) {\r\n" + " if(event.keyCode == 13) {\r\n" + " var pageNum = document.getElementById(\"pagingPageNum\").value;\r\n" + " var form = document.getElementById(\"pagingForm\");\r\n" + " form.page.value = pageNum;\r\n" + " form.submit();\r\n" + " }\r\n" + " }\r\n" + " </script>";return pagingElement + hiddenForm + script;}}
标签库描述文件中添加paging标签
<tag><name>paging</name><tag-class>com.zking.mvc.tag.PagingTag</tag-class><body-content>empty</body-content><attribute><name>pageBean</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag>
使用分页标签
首先在页面中引入标签
<%@taglib prefix="z" uri="/zking" %>
将原来的分页功能,替换为标签即可
<z:paging pageBean="${pageBean}"/>
MySQL分页
-
limit语法
select * from table_name limit [offset,] rows
-
参数说明
参数 | 说明 |
---|---|
offset | 指定第一个返回记录行的偏移量(即从哪一行开始返回),注意:初始行的偏移量为0 |
offset | 返回具体行数 |
最后结果展示页面