使用一个Java Web框架实现简易的待办事项管理系统Todolist,具体要求如下:待办事项Todo应保存的信息包括但不限于编号(Id/整型)、内容(Content/字符串类型)、完成状态(Complete/布尔类型);使用ConcurrentHashMap模拟内存数据库,对待办事项进行储存。要求有四种功能检查状态,编辑,删除,显示。并给出具体代码
时间: 2024-04-09 19:28:07 浏览: 220
to-do-list:一个简单的Java待办事项清单
以下是使用Java Web框架实现简易的待办事项管理系统Todolist的示例代码:
首先,创建一个Todo类,表示待办事项:
```java
public class Todo {
private int id;
private String content;
private boolean complete;
public Todo() {
}
public Todo(int id, String content, boolean complete) {
this.id = id;
this.content = content;
this.complete = complete;
}
// Getter and Setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isComplete() {
return complete;
}
public void setComplete(boolean complete) {
this.complete = complete;
}
}
```
接下来,创建一个TodoList类,用于管理待办事项:
```java
import java.util.concurrent.ConcurrentHashMap;
public class TodoList {
private ConcurrentHashMap<Integer, Todo> todos;
public TodoList() {
todos = new ConcurrentHashMap<>();
}
public void add(Todo todo) {
todos.put(todo.getId(), todo);
}
public void remove(int id) {
todos.remove(id);
}
public Todo get(int id) {
return todos.get(id);
}
public void markAsComplete(int id) {
Todo todo = todos.get(id);
if (todo != null) {
todo.setComplete(true);
}
}
public void markAsIncomplete(int id) {
Todo todo = todos.get(id);
if (todo != null) {
todo.setComplete(false);
}
}
}
```
在Web框架中,创建一个Servlet类来处理HTTP请求和响应:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TodoServlet extends HttpServlet {
private TodoList todoList;
public void init() throws ServletException {
todoList = new TodoList();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String content = request.getParameter("content");
boolean complete = Boolean.parseBoolean(request.getParameter("complete"));
Todo todo = new Todo(id, content, complete);
todoList.add(todo);
response.sendRedirect("index.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
todoList.remove(id);
} else if (action.equals("complete")) {
int id = Integer.parseInt(request.getParameter("id"));
todoList.markAsComplete(id);
} else if (action.equals("incomplete")) {
int id = Integer.parseInt(request.getParameter("id"));
todoList.markAsIncomplete(id);
}
response.sendRedirect("index.jsp");
}
}
```
最后,创建一个index.jsp文件来显示待办事项列表和提供添加操作的表单:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Todolist</title>
</head>
<body>
<h1>Todolist</h1>
<h2>Add New Todo</h2>
<form action="todo" method="post">
<input type="text" name="id" placeholder="Id" required><br>
<input type="text" name="content" placeholder="Content" required><br>
<input type="checkbox" name="complete"> Complete<br>
<input type="submit" value="Add">
</form>
<h2>Todo List</h2>
<table>
<tr>
<th>Id</th>
<th>Content</th>
<th>Complete</th>
<th>Actions</th>
</tr>
<% for (Todo todo : todoList.getTodos()) { %>
<tr>
<td><%= todo.getId() %></td>
<td><%= todo.getContent() %></td>
<td><%= todo.isComplete() %></td>
<td>
<a href="todo?action=delete&id=<%= todo.getId() %>">Delete</a>
<% if (todo.isComplete()) { %>
<a href="todo?action=incomplete&id=<%= todo.getId() %>">Mark as Incomplete</a>
<% } else { %>
<a href="todo?action=complete&id=<%= todo.getId() %>">Mark as Complete</a>
<% } %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
```
以上代码使用Java Web框架实现了一个简易的待办事项管理系统Todolist,包括添加、编辑、删除和显示待办事项的功能。通过ConcurrentHashMap模拟内存数据库来存储待办事项的信息。你可以根据自己的需要进行修改和扩展。
阅读全文