目录
3.存数据 将查询参数传回给JSP 获取调度器 转发到UserList.jsp
用户列表查询设计
UserServlet
1.获取页面传来的请求参数 参数写name的值
2.调用下层拿取数据
3.存数据 将查询参数传回给JSP 获取调度器 转发到UserList.jsp
连接池问题
UserServlet完整代码
import com.iweb.shop.dao.ConnectionUtil;
import com.iweb.shop.dao.UserDao;
import com.iweb.shop.entity.User;
import javax.servlet.RequestDispatcher;
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.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@WebServlet(urlPatterns = "/userlist")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取页面传来的请求参数 参数写name的值
//request.getParameter()取出来的数据一定是String类型,如果需要别的类型 那么需要自己手动转型
String username = request.getParameter("username");
String mobile = request.getParameter("mobile");
String idCode = request.getParameter("idCode");
//如果当前页码为空 默认是第一页
Integer currentPage = request.getParameter("currentPage") == null ? 1 : Integer.parseInt(request.getParameter("currentPage"));
//如果页面容量为空 默认每页显示10条
Integer pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request.getParameter("pageSize"));
//2.调用下层拿取数据
Connection conn = ConnectionUtil.getConnection();
UserDao userDao = UserDao.getInstance(conn);
User user = new User();
user.setUsername(username);
user.setMobile(mobile);
user.setIdCode(idCode);
int start = (currentPage - 1) * pageSize;
try {
int rowCount = userDao.count(user);
int pageCount = rowCount % pageSize == 0 ?
rowCount/pageSize : rowCount/pageSize + 1; //总页数
List<User> userList = userDao.selectList(user, start, pageSize);
//3.把数据存储在request对象中
request.setAttribute("userList",userList);
request.setAttribute("rowCount",rowCount);
request.setAttribute("pageCount",pageCount);
request.setAttribute("currentPage",currentPage); //当前页码
request.setAttribute("pageSize",pageCount); //页面容量
//4.把3个查询参数再传回给jsp (1.用于form表单查询参数回显 2.用于分页器的超链接href携带查询参数)
request.setAttribute("username",username);
request.setAttribute("mobile",mobile);
request.setAttribute("idCode",idCode);
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
//每次查询完需要关掉连接,否则会达到连接池上限会报错
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//5.获取UserList.jsp调度器
RequestDispatcher dispatcher = request.getRequestDispatcher("UserList.jsp");
//6.转发到UserList.jsp
dispatcher.forward(request,response);
}
}
UserJsp完整代码
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户列表</title>
<link rel="stylesheet" href="bootstrap-3.4.1/dist/css/bootstrap.css">
</head>
<body>
<div class="container-fluid" style="padding-top: 50px">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-primary">
<div class="panel-heading">用户列表
<a href="/productlist" style="background-color: pink">商品列表</a>
</div>
<div class="panel-body ">
<form action="/userlist" class="form-inline">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" id="username" name="username" placeholder="支持模糊查询" value="${username}">
</div>
<div class="form-group">
<label for="mobile">手机号</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="支持模糊查询" value="${mobile}">
</div>
<div class="form-group">
<label for="idCode">身份证号</label>
<input type="text" class="form-control" id="idCode" name="idCode" placeholder="支持模糊查询" value="${idCode}">
</div>
<button type="submit" class="btn btn-primary">查询</button>
</form>
<c:if test="${empty userList}">
<div class="jumbotron" style="text-align: center">
<h1 style="color: red"> 暂无数据!</h1>
</div>
</c:if>
<c:if test="${not empty userList}">
<table class="table table-striped table-bordered table-hover text-center">
<thead>
<th>用户编号</th>
<th>用户名</th>
<th>性别</th>
<th>手机号码</th>
<th>身份证号码</th>
</thead>
<tbody>
<%-- el表达式 ${} 用来取值 } --%>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.sex}</td>
<td>${user.mobile}</td>
<td>${user.idCode}</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</div>
<div class="panel-footer" style="display: flex;justify-content: space-between;align-items: center">
<c:if test="${not empty userList}">
<div style="font-weight: bold;">
一共查询到${rowCount}条数据, 当前是第<span class="label label-danger">${currentPage}</span>/${pageCount}页
</div>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="/userlist?currentPage=${currentPage-1 < 1 ? 1 : currentPage-1}&username=${username}&mobile=${mobile}&idCode=${idCode}" >
<span aria-hidden="true">«</span>
</a>
</li>
<c:forEach var="i" step="1" begin="${currentPage - 3 < 1 ? 1 : currentPage - 3}" end="${currentPage - 1}">
<li><a href="/userlist?currentPage=${i}&username=${username}&mobile=${mobile}&idCode=${idCode}">${i}</a></li>
</c:forEach>
<li class="active"><a href="/userlist?currentPage=${currentPage}&username=${username}&mobile=${mobile}&idCode=${idCode}">${currentPage}</a></li>
<c:forEach var="i" step="1"
begin="${currentPage + 1}"
end="${currentPage + 3 > pageCount ? pageCount : currentPage + 3}">
<li><a href="/userlist?currentPage=${i}&username=${username}&mobile=${mobile}&idCode=${idCode}">${i}</a></li>
</c:forEach>
<li>
<a href="/userlist?currentPage=${currentPage+1 > pageCount ? pageCount : currentPage + 1}&username=${username}&mobile=${mobile}&idCode=${idCode}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</c:if>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
效果