实现查询数据分页显示,需要在分页显示功能的基础上,获取用户输入值,并进行 sql 语句的拼接,并且不影响分页数据的正常获取。分页功能的编写在上一篇博客。
Servlet改写
将获取的输入放入 map 集合 condition 中,并调用service查询返回数据集合,然后输入值要通过 request 传回 list.jsp 进行数据回显。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @WebServlet("/findUserByPageServlet") public class FindUserByPageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String currentPage = request.getParameter("currentPage"); String rows = request.getParameter("rows"); if(currentPage == null || "".equals(currentPage)){ currentPage = "1"; } if(rows == null || "".equals(rows)){ rows = "5"; }
Map<String, String[]> codition = request.getParameterMap();
UserSvice service = new UserServiceImpl(); PageBean<User> pb = service.finUserByPage(currentPage,rows,codition); request.setAttribute("pb",pb); request.setAttribute("condition",codition); request.getRequestDispatcher("/list.jsp").forward(request,response); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
|
Service 改写
调用 dao 查询总记录数增加参数 condition ,如果 condition 有值则为搜索状态,获得符合查询条件的总记录数,没有搜索时为空,获得总记录数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Override public PageBean<User> finUserByPage(String _currentPage, String _rows, Map<String, String[]> codition) { int currentPage = Integer.parseInt(_currentPage); int rows = Integer.parseInt(_rows); if(currentPage <= 0){ currentPage = 1; } PageBean<User> pb = new PageBean<User>(); pb.setCurrentPage(currentPage); pb.setRows(rows); int totalCount = dao.findTotalCount(codition); pb.setTotalCount(totalCount); int start = (currentPage - 1) * rows; List<User> list = dao.findByPage(start,rows,codition); pb.setList(list); int totalPage = totalCount % rows == 0 ? totalCount/rows : totalCount/rows + 1; pb.setTotalPage(totalPage); return pb; }
|
Dao方法改写
改写原 sql 语句为了拼接后续条件语句,添加 where 1 = 1 。当 condition 中的值不为空时,进行 sql 语句拼接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @Override public List<User> findByPage(int start, int rows, Map<String, String[]> codition) { String sql = "select * from user where 1 = 1"; StringBuilder sb = new StringBuilder(sql); Set<String> Keyset = codition.keySet(); List<Object> params = new ArrayList<Object>(); for (String key : Keyset) { if ("currentPage".equals(key) || "rows".equals(key)){ continue; } String value = codition.get(key)[0]; if (value != null && !"".equals(value)){ sb.append(" and "+key+" like ? "); params.add("%"+value+"%"); } } sb.append(" limit ?,? "); params.add(start); params.add(rows); return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray()); }
|
前端页面改写
查询框:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" > </div> <div class="form-group"> <label for="exampleInputName3">籍贯</label> <input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputName3" > </div> <div class="form-group"> <label for="exampleInputEmail2">邮箱</label> <input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail2" > </div> <button type="submit" class="btn btn-default">查询</button> </form>
|
分页框:
须在页码、上一页、下一页的点击链接上添加用户输入的值,然后才能在查询出的页面进行正常跳转。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <div> <nav aria-label="Page navigation"> <ul class="pagination"> <c:if test="${pb.currentPage == 1}"><li class="disabled"></c:if> <c:if test="${pb.currentPage != 1}"><li></c:if> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <c:forEach begin="1" end="${pb.totalPage}" var="i"> <c:if test="${pb.currentPage == i}"> <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li> </c:if> <c:if test="${pb.currentPage != i}"> <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li> </c:if> </c:forEach> <c:if test="${pb.currentPage == pb.totalPage}"><li class="disabled"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li></c:if> <c:if test="${pb.currentPage < pb.totalPage}"><li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </c:if>
<span style="font-size: 25px;margin-left: 5px"> 共${pb.totalCount}条记录,共${pb.totalPage}页 </span> </ul> </nav> </div>
|