帮我找javawebMVC模式实现CRUD操作代码及运行图
时间: 2024-01-02 22:04:08 浏览: 121
javaweb-mvc-crud
好的,以下是一个简单的JavaWeb MVC模式实现CRUD操作的代码示例:
Model类(User.java):
```java
public class User {
private int id;
private String name;
private int age;
public User() {}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// getter and setter methods
// ...
}
```
DAO类(UserDAO.java):
```java
public class UserDAO {
private Connection conn;
public UserDAO() {
// 初始化数据库连接
// ...
}
public List<User> getAllUsers() {
List<User> users = new ArrayList<User>();
// 执行查询语句,并将结果封装到List<User>中
// ...
return users;
}
public User getUserById(int id) {
User user = null;
// 执行查询语句,并将结果封装到User对象中
// ...
return user;
}
public void addUser(User user) {
// 执行插入语句,将User对象中的数据插入到数据库中
// ...
}
public void updateUser(User user) {
// 执行更新语句,更新数据库中对应的数据
// ...
}
public void deleteUser(int id) {
// 执行删除语句,删除数据库中对应的数据
// ...
}
// 关闭数据库连接
// ...
}
```
Controller类(UserController.java):
```java
public class UserController extends HttpServlet {
private UserDAO userDao;
public UserController() {
userDao = new UserDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getServletPath();
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/delete":
deleteUser(request, response);
break;
case "/view":
viewUser(request, response);
break;
default:
listUsers(request, response);
break;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getServletPath();
switch (action) {
case "/new":
addUser(request, response);
break;
case "/edit":
updateUser(request, response);
break;
default:
break;
}
}
private void listUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = userDao.getAllUsers();
request.setAttribute("users", users);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-list.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
dispatcher.forward(request, response);
}
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 addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
User newUser = new User(0, name, age);
userDao.addUser(newUser);
response.sendRedirect("list");
}
private 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 existingUser = new User(id, name, age);
userDao.updateUser(existingUser);
response.sendRedirect("list");
}
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userDao.deleteUser(id);
response.sendRedirect("list");
}
private void viewUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
User user = userDao.getUserById(id);
request.setAttribute("user", user);
RequestDispatcher dispatcher = request.getRequestDispatcher("user-view.jsp");
dispatcher.forward(request, response);
}
}
```
View视图(user-list.jsp、user-form.jsp、user-view.jsp):
user-list.jsp:
```html
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Actions</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>
<a href="view?id=${user.id}">View</a>
<a href="edit?id=${user.id}">Edit</a>
<a href="delete?id=${user.id}" onclick="return confirm('Are you sure you want to delete this user?')">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="new">Add User</a>
</body>
</html>
```
user-form.jsp:
```html
<html>
<head>
<title>User Form</title>
</head>
<body>
<h1>User Form</h1>
<form action="${action}" method="POST">
<c:if test="${not empty user.id}">
<input type="hidden" name="id" value="${user.id}">
</c:if>
<label for="name">Name:</label>
<input type="text" name="name" value="${not empty user.name ? user.name : ''}">
<br>
<label for="age">Age:</label>
<input type="number" name="age" value="${not empty user.age ? user.age : ''}">
<br>
<input type="submit" value="Save">
<a href="list">Cancel</a>
</form>
</body>
</html>
```
user-view.jsp:
```html
<html>
<head>
<title>User View</title>
</head>
<body>
<h1>User View</h1>
<table border="1">
<tr>
<th>ID</th>
<td>${user.id}</td>
</tr>
<tr>
<th>Name</th>
<td>${user.name}</td>
</tr>
<tr>
<th>Age</th>
<td>${user.age}</td>
</tr>
</table>
<br>
<a href="list">Back to List</a>
</body>
</html>
```
这里只是简单的示例,实际上还需要进行一些参数验证、异常处理等操作,以确保系统的安全和稳定性。
至于运行图,由于我无法直接在这里插入图片,您可以在本地运行代码后,在浏览器中查看效果。
阅读全文