了解了,你使用的是JSP和MySQL。针对你的需求,我们在JSP页面和Java后台代码中进行必要的修改,以确保只有管理员可以编辑和删除论坛帖子。
### 数据库设置
假设你已经创建了`users`表和`posts`表:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
is_admin BOOLEAN DEFAULT FALSE
);
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
author_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id)
);
-- 创建一个管理员用户
INSERT INTO users (username, password, is_admin) VALUES ('admin', MD5('adminpassword'), TRUE);
### 管理员登录页面 (`admin_login.jsp`)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>管理员登录</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h2>管理员登录</h2>
<form action="login.jsp" method="post">
用户名: <input type="text" name="username" required>
密码: <input type="password" name="password" required>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
### 登录处理 (`login.jsp`)
<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.*, javax.servlet.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT id, is_admin FROM users WHERE username = ? AND password = MD5(?)";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
int userId = rs.getInt("id");
boolean isAdmin = rs.getBoolean("is_admin");
HttpSession session = request.getSession();
session.setAttribute("userId", userId);
session.setAttribute("isAdmin", isAdmin);
response.sendRedirect("forum.jsp");
} else {
out.println("用户名或密码错误!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
}
%>
### 论坛页面 (`forum.jsp`)
<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.*, javax.servlet.*" %>
<%
HttpSession session = request.getSession(false);
boolean isAdmin = false;
if (session != null && session.getAttribute("isAdmin") != null) {
isAdmin = (boolean) session.getAttribute("isAdmin");
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT posts.id, posts.title, posts.content, posts.created_at, users.username FROM posts JOIN users ON posts.author_id = users.id ORDER BY posts.created_at DESC";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
%>
<!DOCTYPE html>
<html>
<head>
<title>简单论坛</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h2>简单论坛</h2>
<form action="add_post.jsp" method="post" onsubmit="return validateForm();">
帖子标题: <input type="text" name="title" required>
帖子内容: <textarea name="content" required></textarea>
<button type="submit">发表</button>
</form>
<h3>帖子列表</h3>
<% while (rs.next()) { %>
<div class="post">
<h3><%= rs.getString("title") %></h3>
<p><%= rs.getString("content") %></p>
<small> 发表于: <%= rs.getTimestamp("created_at") %> | 作者: <%= rs.getString("username") %></small>
<% if (isAdmin) { %>
<form action="edit_post.jsp" method="get">
<input type="hidden" name="id" value="<%= rs.getInt("id") %>">
<button type="submit">编辑</button>
</form>
<form action="delete_post.jsp" method="post">
<input type="hidden" name="id" value="<%= rs.getInt("id") %>">
<button type="submit">删除</button>
</form>
<% } %>
</div>
<% } %>
</div>
</body>
</html>
<%
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
%>
### 删除帖子 (`delete_post.jsp`)
<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.*, javax.servlet.*" %>
<%
int postId = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM posts WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, postId);
ps.executeUpdate();
response.sendRedirect("forum.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) ps.close();
if (conn != null) conn.close();
}
%>
### 编辑帖子 (`edit_post.jsp`)
<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.*, javax.servlet.*" %>
<%
int postId = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT title, content FROM posts WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, postId);
rs = ps.executeQuery();
if (rs.next()) {
%>
<!DOCTYPE html>
<html>
<head>
<title>编辑帖子</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<h2>编辑帖子</h2>
<form action="update_post.jsp" method="post">
<input type="hidden" name="id" value="<%= postId %>">
帖子标题: <input type="text" name="title" value="<%= rs.getString("title") %>" required>
帖子内容: <textarea name="content" required><%= rs.getString("content") %></textarea>
<button type="submit">保存</button>
</form>
</div>
</body>
</html>
<%
} else {
out.println("找不到指定的帖子!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
}
%>
### 更新帖子 (`update_post.jsp`)
<%@ page import="java.sql.*" %>
<%@ page import="javax.servlet.http.*, javax.servlet.*" %>
<%
int postId = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String content = request.getParameter("content");
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE posts SET title = ?, content = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, title);
ps.setString(2, content);
ps.setInt(3, postId);
ps.executeUpdate();
response.sendRedirect("forum.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) ps.close();
if (conn != null) conn.close();
}
%>
### 数据库工具类 (`DBUtil.java`)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/YourDatabaseName?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
private static final String USER = "yourDbUsername";
private static final String PASSWORD = "yourDbPassword";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
通过这些代码改动,你现在有了一个支持管理员管理帖子的论坛系统,其中只有管理员可以编辑和删除帖子。调整和运行这些代码后,如果还有任何问题或需要进一步帮助,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |