好的,为了提高论坛的功能和安全性,我们将添加管理员用户,管理员可以编辑和删除帖子。我们需要更新数据库结构、创建管理员登录功能,并在论坛页面中添加编辑和删除功能。
### 数据库结构更新
首先,更新数据库结构,添加一个管理员用户:
-- 添加一个管理员用户
INSERT INTO users (username, password) VALUES ('admin', 'adminpassword'); -- 请确保密码已加密
-- 更新帖子表以支持编辑和删除功能
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
### 管理员登录页面 (`admin_login.jsp`)
创建一个管理员登录页面:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>管理员登录</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h2>管理员登录</h2>
<form method="post" action="admin_login.jsp">
用户名: <input type="text" name="username" required><br>
密码: <input type="password" name="password" required><br>
<button type="submit">登录</button>
</form>
<%
if (request.getMethod().equalsIgnoreCase("POST")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证管理员身份
if ("admin".equals(username) && "adminpassword".equals(password)) {
session.setAttribute("adminLoggedIn", true);
response.sendRedirect("forum.jsp");
} else {
out.println("<p style='color:red;'>用户名或密码错误</p>");
}
}
%>
</div>
</body>
</html>
### 更新后的`forum.jsp`
让我们在论坛页面中添加编辑和删除功能:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>简单论坛</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h2>简单论坛</h2>
<form method="post" action="forum.jsp" onsubmit="return validateForm();">
帖子标题: <input type="text" name="title" required><br>
帖子内容: <textarea name="content" required></textarea><br>
<button type="submit">发表</button>
</form>
<h3>帖子列表</h3>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
if (request.getMethod().equalsIgnoreCase("POST") && session.getAttribute("adminLoggedIn") == null) {
String title = request.getParameter("title");
String content = request.getParameter("content");
String username = (String) session.getAttribute("username");
pstmt = conn.prepareStatement("INSERT INTO posts (username, title, content) VALUES (?, ?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, title);
pstmt.setString(3, content);
pstmt.executeUpdate();
}
if (session.getAttribute("adminLoggedIn") != null && request.getParameter("edit") != null) {
int postId = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String content = request.getParameter("content");
pstmt = conn.prepareStatement("UPDATE posts SET title=?, content=? WHERE id=?");
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setInt(3, postId);
pstmt.executeUpdate();
response.sendRedirect("forum.jsp");
}
if (session.getAttribute("adminLoggedIn") != null && request.getParameter("delete") != null) {
int postId = Integer.parseInt(request.getParameter("id"));
pstmt = conn.prepareStatement("DELETE FROM posts WHERE id=?");
pstmt.setInt(1, postId);
pstmt.executeUpdate();
response.sendRedirect("forum.jsp");
}
pstmt = conn.prepareStatement("SELECT * FROM posts ORDER BY created_at DESC");
rs = pstmt.executeQuery();
while (rs.next()) {
int postId = rs.getInt("id");
String postTitle = rs.getString("title");
String postContent = rs.getString("content");
String postTime = rs.getTimestamp("created_at").toString();
out.println("<div class='post'>");
out.println("<h3>" + postTitle + "</h3>");
out.println("<p>" + postContent + "</p>");
out.println("<small>发表于: " + postTime + "</small>");
if (session.getAttribute("adminLoggedIn") != null) {
out.println("<form method='post' action='forum.jsp'>");
out.println("<input type='hidden' name='id' value='" + postId + "'>");
out.println("<input type='text' name='title' value='" + postTitle + "'>");
out.println("<textarea name='content'>" + postContent + "</textarea>");
out.println("<button type='submit' name='edit'>编辑</button>");
out.println("<button type='submit' name='delete'>删除</button>");
out.println("</form>");
}
out.println("</div>");
}
} catch (SQLException e) {
e.printStackTrace();
out.println("数据库操作出现问题:" + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
</div>
<script>
function validateForm() {
var title = document.forms["form"].value;
var content = document.forms["form"].value;
if (title.length < 1) {
alert("标题不能为空");
return false;
}
if (content.length < 1) {
alert("内容不能为空");
return false;
}
return true;
}
</script>
</body>
</html>
### CSS 样式文件 (`styles.css`)
/* styles.css */
/* 通用样式 */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
background-image: url('background.jpg'); /* 用于背景图片 */
background-size: cover;
background-position: center;
margin: 0;
padding: 0;
}
.container {
background-color: rgba(255, 255, 255, 0.9);
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 600px;
max-width: 100%;
text-align: center;
margin: 50px auto;
}
h2, h3 {
margin-bottom: 20px;
color: #333;
}
form {
margin-bottom: 30px;
}
input,
textarea {
width: calc(100% - 22px);
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
width: 100%;
padding: 10px;
background-color: #5cb85c;
border: none;
border-radius: 3px;
color: white;
font-size: 16px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #4cae4c;
}
.post {
background-color: #fff;
padding: 15px;
border-radius: 3px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
text-align: left;
}
.post h3 {
margin-top: 0;
}
.post small {
color: #888;
display: block;
text-align: right;
}
### 总结
1. **管理员登录页面 (`admin_login.jsp`)**:添加管理员登录功能。
2. **更新 `forum.jsp`**:添加编辑和删除帖子功能,确保管理员可以管理帖子。
3. **CSS 样式文件 (`styles.css`)**:美化页面并添加背景图片。
通过这些更改,你现在有一个基本的论坛系统,可以让管理员用户管理帖子。如果你有任何其他问题或者需要进一步调整,请随时告诉我!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |