用户信息查询功能
1. 网页设计
本能地想找一个表格的模板,奈何模板之家里并没用,所以只好自己写一个界面了。。ค(TㅅT)
界面极其简陋,凑合着用。。
2. 后台代码实现
在实现查询功能之前,先分析一下:在(二)中,管理员登录完成后重定向到FindServletByPage这个servlet中,我们应该在这个servlet里面实现分页查询的功能,相比较查询所有信息,分页查询每次只查询一定数量的信息并且显示在界面中,使得界面条理清晰,更友好,而查询所有信息并且显示在一个界面上,显然当数据庞大时,就显得界面冗长了。因此,我们将一个Page封装成一个JavaBean。
PageBean类
package cn.hehewocao.POJO;
import java.util.List;
public class PageBean<T> {
private int page; //当前页数
private int row; // 分页条数
private List<T> list; //查询的每页记录
private int count; //总记录数
private int pages; //展示数据的总页数
public PageBean() {
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
@Override
public String toString() {
return "PageBean{" +
"page=" + page +
", row=" + row +
", list=" + list +
", count=" + count +
", pages=" + pages +
'}';
}
}
分页查询Servlet:FindServletByPage
package cn.hehewocao.Servlet;
import cn.hehewocao.POJO.User;
import cn.hehewocao.Service.UserService;
import cn.hehewocao.Service.UserServiceImp;
import org.apache.commons.beanutils.BeanUtils;
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 java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import cn.hehewocao.POJO.PageBean;
@WebServlet("/findServletByPage")
public class FindServletByPage extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String page = req.getParameter("page");
String rows = req.getParameter("rows");
Map<String, String[]> parameterMap = req.getParameterMap();//查询条件
UserService us = new UserServiceImp();
PageBean<User> pb = us.findByPage(page, rows, parameterMap);
req.setAttribute("pageBean", pb);//保存查询后的PageBean对象
req.setAttribute("condition",parameterMap);//将查询条件保存到request中,以便回显
req.getRequestDispatcher("list.jsp").forward(req, resp);//转发到list.jsp显示数据
}
}
UserServiceImp类中添加方法:
public PageBean<User> findByPage(String _page, String _rows, Map<String,String[]> info) {//封装PageBean对象
PageBean<User> pb = new PageBean<User>();
//设置默认值
if ("".equals(_page) || _page == null) {
_page = "1";
}
if ("".equals(_rows) || _rows == null) {
_rows = "5";
}
int page = Integer.parseInt(_page);
int rows = Integer.parseInt(_rows);
if(page < 1){
page = 1;
}
if(rows < 1){
rows = 1;
}
//设置查询的页数和每页的记录条数
pb.setPage(page);
pb.setRow(rows);
UserDao ud = new UserDaoImp();
int count = ud.findCount(info);
//总记录数
pb.setCount(count);
int startIndex = (page - 1) * rows;
List<User> list = ud.findByPage(startIndex,rows,info);
if (list == null || list.size() == 0){
return null;
}
//该页记录集合
pb.setList(list);
//页数
int pages = count % rows == 0 ? count/rows : count/rows + 1;
pb.setPages(pages);
return pb;
}
UserDaoImp类中添加方法:
public int findCount(Map<String, String[]> info) {//查询总记录数
/*初始化sql语句*/
String sql = "select count(*) from user where 1 = 1 ";
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDateSource());
List<String> list = new ArrayList<String>();
for (String key : info.keySet()) {
if (!"".equals(info.get(key)[0]) && info.get(key)[0] != null) {
if (!key.equals("page")) {
if (!key.equals("rows")) {
sql += " and " + key + " like ? ";
list.add("%" + info.get(key)[0] + "%");
}
}
}
}
return jdbcTemplate.queryForObject(sql, list.toArray(), int.class);
}
3. JSP页面设计
该界面主要就是从request中获取PageBean对象中保存的数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>用户管理</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
<style>
tr, th, td {
text-align: center;
}
</style>
</head>
<body>
<div style="width: 70%;margin-left: 15%;margin-top: 50px;" align="center">
<form class="form-inline" action="${pageContext.request.contextPath}/findServletByPage" method="post">
<div class="form-group">
<label>编号</label>
<input id="id" type="text" name="id" value="${condition.id[0]}" placeholder="输入编号" align="center"/>
</div>
<div class="form-group">
<label>用户名</label>
<input id="username" type="text" value="${condition.username[0]}" name="username" placeholder="输入用户名" align="center"/>
</div>
<div class="form-group">
<label>昵称</label>
<input type="text" id="nickname" value="${condition.nickname[0]}" name="nickname" placeholder="昵称" align="center"/>
</div>
<div class="form-group">
<label>地址</label>
<input type="text" id="address" value="${condition.address[0]}" name="address" placeholder="地址" align="center"/>
</div>
<button type="submit" class="btn btn-default" id="find">查询</button>
<a href="register.jsp"><button type="button" class="btn btn-primary">添加用户</button></a>
</form>
</div>
<div style="width: 80%;margin-left: 10%;margin-top: 50px;">
<table class="table table-hover">
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>昵称</th>
<th>性别</th>
<th>生日</th>
<th>手机号</th>
<th>邮箱</th>
<th>地址</th>
<th>职业</th>
<th>描述</th>
<th colspan="2">操作</th>
</tr>
<c:if test="${not empty pageBean}">
<c:forEach items="${pageBean.list}" var="user" begin="0" end="${pageBean.list.size() - 1}" step="1"
varStatus="s">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.nickname}</td>
<td>${user.sex}</td>
<td>${user.birthday}</td>
<td>${user.phone}</td>
<td>${user.email}</td>
<td>${user.address}</td>
<td>${user.occupation}</td>
<td>${user.describes}</td>
<td><a href="${pageContext.request.contextPath}/updateUserForwordServlet?beforeUserId=${user.id}" name="del" id="update${s}">修改</a></td>
<td><a href="${pageContext.request.contextPath}/delServletById?delId=${user.id}" name="del" id="${s}"
onclick="delLine(this)">删除</a></td>
</tr>
</c:forEach>
</c:if>
</table>
</div>
<%--分页栏--%>
<div align="center" style="margin-top: 50px;">
<nav aria-label="Page navigation">
<ul class="pagination">
<c:if test="${pageBean.page == 1}">
<li class="disabled">
<a href="#"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.page != 1}">
<li>
<a href="${pageContext.request.contextPath}/findServletByPage?page=${pageBean.page - 1}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach begin="1" end='${pageContext.request.getAttribute("pageBean").pages}' var="i">
<c:if test="${pageBean.page == i}">
<li class="active"><a
href='${pageContext.request.contextPath}/findServletByPage?page=${i}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}'>${i}</a></li>
</c:if>
<c:if test="${pageBean.page != i}">
<li><a href='${pageContext.request.contextPath}/findServletByPage?page=${i}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}'>${i}</a></li>
</c:if>
</c:forEach>
<c:if test="${pageBean.pages == pageBean.page}">
<li class="disabled">
<a href="#"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.pages != pageBean.page}">
<li>
<a href="${pageContext.request.contextPath}/findServletByPage?page=${pageBean.page + 1}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&$address=${condition.address[0]}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
</nav>
<p style="font-weight: bold">
共${pageBean.count == null ? 0 : pageBean.count}条记录,共${pageBean.pages == null ? 0 : pageBean.pages}页</p>
</div>
<script>
function delLine(obj) {
if (confirm("确认删除?")) {
return true;
}
}
</script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
至此就实现了用户信息的分页查询功能。