用mvc模式实现jsp增删改查
时间: 2024-01-05 17:04:38 浏览: 195
MVC实现增删改查的实例
5星 · 资源好评率100%
MVC是一种软件架构模式,它将应用程序分为三个主要部分:模型、视图和控制器。下面是一个使用MVC模式实现JSP增删改查的示例:
1. 创建一个JavaBean作为模型:
```
public class User {
private int id;
private String name;
private String email;
// getter和setter方法
}
```
2. 创建一个控制器:
```
@WebServlet(name = "UserController", urlPatterns = {"/users"})
public class UserController extends HttpServlet {
private UserDao userDao;
public void init() {
userDao = new UserDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "new":
showNewForm(request, response);
break;
case "insert":
insertUser(request, response);
break;
case "delete":
deleteUser(request, response);
break;
case "edit":
showEditForm(request, response);
break;
case "update":
updateUser(request, response);
break;
default:
listUser(request, response);
break;
}
}
// 显示添加用户表单
private void showNewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
dispatcher.forward(request, response);
}
// 处理添加用户请求
private void insertUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
User newUser = new User(name, email);
userDao.addUser(newUser);
response.sendRedirect("users?action=list");
}
// 删除用户
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userDao.deleteUser(id);
response.sendRedirect("users?action=list");
}
// 显示编辑用户表单
private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
User existingUser = userDao.getUserById(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
request.setAttribute("user", existingUser);
dispatcher.forward(request, response);
}
// 更新用户
private void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String email = request.getParameter("email");
User user = new User(id, name, email);
userDao.updateUser(user);
response.sendRedirect("users?action=list");
}
// 列出所有用户
private void listUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> listUser = userDao.getAllUsers();
request.setAttribute("listUser", listUser);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-list.jsp");
dispatcher.forward(request, response);
}
}
```
3. 创建一个视图:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<a href="users?action=new">Add User</a>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listUser}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.email}</td>
<td>
<a href="users?action=edit&id=${user.id}">Edit</a>
<a href="users?action=delete&id=${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
4. 创建一个包含表单的视图:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Form</title>
</head>
<body>
<h1>User Form</h1>
<form method="post" action="users">
<input type="hidden" name="action" value="${user.id == null ? 'insert' : 'update'}">
<input type="hidden" name="id" value="${user.id}">
<label for="name">Name</label>
<input type="text" name="name" value="${user.name}">
<br>
<label for="email">Email</label>
<input type="text" name="email" value="${user.email}">
<br>
<button type="submit">Save</button>
<a href="users">Cancel</a>
</form>
</body>
</html>
```
以上就是一个使用MVC模式实现JSP增删改查的示例。该示例将应用程序分为三个主要部分:模型、视图和控制器。模型代表数据,控制器处理用户请求并更新模型,视图呈现模型数据给用户。使用MVC模式可以使应用程序更易于维护和扩展。
阅读全文