使用mvc模式实现增删改查,写出具体代码和前端页面
时间: 2024-02-29 13:53:13 浏览: 67
好的,以下是使用MVC模式实现增删改查的代码和前端页面示例:
Model层代码:
```java
public class User {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
public class UserDao {
// 添加用户
public void addUser(User user) {
// 数据库操作,执行插入语句
}
// 根据ID删除用户
public void deleteUserById(int id) {
// 数据库操作,执行删除语句
}
// 更新用户信息
public void updateUser(User user) {
// 数据库操作,执行更新语句
}
// 根据ID查询用户信息
public User getUserById(int id) {
// 数据库操作,执行查询语句
// 将查询结果封装成User对象并返回
return user;
}
}
```
View层代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户管理</title>
</head>
<body>
<h1>用户管理</h1>
<hr>
<h2>添加用户</h2>
<form method="post" action="UserController?action=addUser">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="添加">
</form>
<hr>
<h2>删除用户</h2>
<form method="post" action="UserController?action=deleteUser">
ID:<input type="text" name="id"><br>
<input type="submit" value="删除">
</form>
<hr>
<h2>更新用户信息</h2>
<form method="post" action="UserController?action=updateUser">
ID:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="更新">
</form>
<hr>
<h2>查询用户信息</h2>
<form method="post" action="UserController?action=getUser">
ID:<input type="text" name="id"><br>
<input type="submit" value="查询">
</form>
</body>
</html>
```
Controller层代码:
```java
public class UserController {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
// 添加用户
public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的信息
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
User user = new User();
user.setName(name);
user.setAge(age);
// 调用Model层的方法添加用户
userDao.addUser(user);
// 返回结果页面
request.getRequestDispatcher("result.jsp").forward(request, response);
}
// 根据ID删除用户
public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的ID
int id = Integer.parseInt(request.getParameter("id"));
// 调用Model层的方法删除用户
userDao.deleteUserById(id);
// 返回结果页面
request.getRequestDispatcher("result.jsp").forward(request, response);
}
// 更新用户信息
public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的信息
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
// 调用Model层的方法更新用户信息
userDao.updateUser(user);
// 返回结果页面
request.getRequestDispatcher("result.jsp").forward(request, response);
}
// 根据ID查询用户信息
public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的ID
int id = Integer.parseInt(request.getParameter("id"));
// 调用Model层的方法查询用户信息
User user = userDao.getUserById(id);
// 将查询结果设置到request中
request.setAttribute("user", user);
// 跳转到结果页面,显示用户信息
request.getRequestDispatcher("result.jsp").forward(request, response);
}
}
```
在使用时,可以将UserController注册到Servlet中,并将UserDao注入到UserController中,然后通过前端页面中的表单向Servlet发送请求,Servlet再根据请求的action参数调用相应的方法,最后返回结果页面:
```java
public class MainServlet extends HttpServlet {
private UserController controller;
public void init() {
controller = new UserController();
UserDao userDao = new UserDao();
controller.setUserDao(userDao);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("addUser".equals(action)) {
controller.addUser(request, response);
} else if ("deleteUser".equals(action)) {
controller.deleteUser(request, response);
} else if ("updateUser".equals(action)) {
controller.updateUser(request, response);
} else if ("getUser".equals(action)) {
controller.getUser(request, response);
}
}
}
```
结果页面代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作结果</title>
</head>
<body>
<h1>操作结果</h1>
<hr>
<%
User user = (User)request.getAttribute("user");
if (user != null) {
%>
<h2>查询结果</h2>
用户ID:<%=user.getId()%><br>
用户姓名:<%=user.getName()%><br>
用户年龄:<%=user.getAge()%><br>
<%
} else {
%>
操作成功!
<%
}
%>
<hr>
<a href="index.jsp">返回</a>
</body>
</html>
```
阅读全文