add<用户列表>

master
liyansheng 2024-12-21 12:31:42 +08:00
parent e6ea47674d
commit 31c203136e
5 changed files with 101 additions and 30 deletions

View File

@ -0,0 +1,55 @@
package example.controller;
import example.dao.UserDao;
import example.model.User;
import example.service.IUserService;
import example.service.impl.UserServiceImpl;
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.util.List;
@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends HttpServlet {
IUserService userService=new UserServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
switch (action) {
case "list":
try {
getUserList(req, resp);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case "update":
req.getRequestDispatcher("/WEB-INF/jsp/register.jsp").forward(req, resp);
break;
case "delete":
resp.sendRedirect("/");
break;
default:
req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp);
break;
}
super.doGet(req, resp);
}
private void getUserList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
List<User> userList = userService.getUserList();
req.setAttribute("userList", userList);
req.getRequestDispatcher("userList.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}

View File

@ -1,5 +1,11 @@
package example.service;
import example.model.User;
import java.util.List;
public interface IUserService {
boolean login(String username, String password) throws Exception;
List<User> getUserList() throws Exception;
}

View File

@ -4,6 +4,8 @@ import example.dao.UserDao;
import example.model.User;
import example.service.IUserService;
import java.util.List;
public class UserServiceImpl implements IUserService {
UserDao userDao=new UserDao();
@ -12,4 +14,9 @@ public class UserServiceImpl implements IUserService {
User user = userDao.getUserByUsername(username);
return user != null && user.getPassword().equals(password);
}
@Override
public List<User> getUserList() throws Exception {
return userDao.getAllUser();
}
}

View File

@ -7,5 +7,6 @@
<a href="/register">注册</a>
<a href="/login">登录</a>
<a href="/computerList">电脑列表</a>
<a href="/user?action=list">用户列表</a>
</body>
</html>

View File

@ -1,38 +1,40 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="example.model.User" %>
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>用户列表</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>user List</title>
</head>
<body>
<h1>用户列表</h1>
<form action="userList" method="get">
<div class="form-group">
<input type="text" class="form-control" name="query" placeholder="请输入用户名进行查询" value="${query}">
<button type="submit" class="btn btn-primary">查询</button>
</div>
</form>
<table border="1" class="table table-striped">
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
</tr>
<c:forEach items="${userList}" var="user">
<h1>user List</h1>
<table border="1">
<thead>
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<th>ID</th>
<th>用户名</th>
<th>手机号</th>
<th>身份标识</th>
<th>Actions</th>
</tr>
</c:forEach>
</thead>
<tbody>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.phone}</td>
<td>${user.admin}</td>
<td>
<a href="/edituser?id=${user.id}">Edit</a> |
<a href="/deleteuser?id=${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br>
当前第 ${currentPage} 页,共 ${totalPages} 页
<div class="pagination-container">
${pageLinks}
</div>
<br/>
<!-- <a href="/adduser">Add New user</a> -->
</body>
</html>
</html>