项目介绍
本系统分为前后台,前台主要功能为:
用户注册登陆、浏览帖子、发布帖子、个人信息管理、消息通知管理,积分管理,
后台管理功能有:
友情链接管理、板块管理、主题管理、用户管理、网站设置等等。本系统实现的功能非常丰富,可自由切换电脑版和手机版,访问时自动识别设备,也可按照ip来设置访问权限,已经达到了正式上线的系统水平,非常值得学习参考。本项目包含完整的源码、数据库文件。
管理员账号/密码:admin/123456
用户账号/密码: userloop/123456
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
技术栈
1. 后端:Spring SpringMVC MyBatis
2. 前端:JSP+bootstrap+jQuery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,下载所需要的jar包;
3. 将项目中beans.xml配置文件中的数据库配置改为自己的配置,主要用户名、密码;
4. 配置tomcat,然后运行运行项目,输入http://localhost:8080/ 登录
用户管理控制层:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private LoginLogService loginLogService;
// 用户登录
@RequestMapping(value = "/userLogin", method = RequestMethod.POST)
public String userLogin(User loginUser, HttpServletRequest request, RedirectAttributes redirect) {
// 通过用户名查找User对象
User user = userService.getUserByUserName(loginUser.getUserName());
String password = "";
if (user != null) {
password = userService.getPassword(user.getUserName());
}
// 判断登录信息是否正确
if (user != null && loginUser.getPassword().equals(password)) {
// 获取登录基本信息
String lastIp = request.getRemoteAddr();
String userName = user.getUserName();
Timestamp lastLoginTime = new Timestamp(new Date().getTime());
// 更新用户信息
user.setLastIp(lastIp);
user.setLastLoginTime(lastLoginTime);
user.setCredit(5 + user.getCredit());
userService.updateUserByUserName(user);
// 更新用户登录日志
UserLoginLog userLoginLog = new UserLoginLog();
userLoginLog.setUserName(userName);
userLoginLog.setLoginIp(lastIp);
userLoginLog.setLoginDateTime(lastLoginTime);
loginLogService.addUserLoginLog(userLoginLog);
// 登陆成功,跳转到主页
request.getSession().setAttribute("username", user.getUserName());
return "redirect:/main";
}
// 登录失败,跳转页面
request.setAttribute("Msg", "登录失败");
return "error";
}
// 用户注册
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String userRegister(User userRegister, HttpServletRequest request) {
User user = userRegister;
if (user != null) {
try {
String username = user.getUserName();
String ip = request.getRemoteAddr();
// 如果数据库中没有该用户,可以注册,否则跳转页面
if (userService.getUserByUserName(username) == null) {
// 添加用户
user.setLastIp(ip);
Timestamp createLoginTime = new Timestamp(new Date().getTime());
user.setCreateTime(createLoginTime);
user.setLastLoginTime(createLoginTime);
userService.addUser(user);
// 添加用户登录日志
UserLoginLog userLoginLog = new UserLoginLog();
userLoginLog.setUserName(username);
userLoginLog.setLoginIp(ip);
userLoginLog.setLoginDateTime(createLoginTime);
loginLogService.addUserLoginLog(userLoginLog);
// 注册成功跳转
request.setAttribute("username", username);
return "index";
} else {
request.setAttribute("Msg", "注册失败,用户名已被占用!");
return "error";
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("Msg", "发生未知错误!");
return "error";
}
}
request.setAttribute("Msg", "发生未知错误!");
return "error";
}
// 显示个人信息
@RequestMapping(value = "/listUserInfo")
public String listUserInfo(String username, HttpServletRequest request) {
User user = userService.getUserByUserName(username);
request.setAttribute("user", user);
return "user/userInfo";
}
// 修改个人信息页面
@RequestMapping(value = "/userUpdateInfo", method = RequestMethod.GET)
public String userUpdateInfoPage(String username, HttpServletRequest request) {
User user = userService.getUserByUserName(username);
request.setAttribute("user", user);
return "user/userUpdateInfo";
}
// 提交用户修改信息
@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
public String updateUserInfo(User user, RedirectAttributes redirectAttributes) {
User newUser = user;
userService.updateUserByUserName(newUser);
redirectAttributes.addAttribute("username", newUser.getUserName());
return "redirect:listUserInfo";
}
// 用户注销功能
@RequestMapping(value = "/loginOut")
public String loginOut(HttpServletRequest request) {
request.getSession().removeAttribute("username");
return "index";
}
}
管理员管理控制层:
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
@Autowired
BoardService boardService;
@Autowired
UserService userService;
@Autowired
PostService postService;
@Autowired
ReplyService replyService;
// 论坛管理中心
@RequestMapping(value = "/manageCenter")
public String manageCenter(HttpServletRequest request) {
if (request.getSession().getAttribute("username").equals("admin")) {
return "admin/manageCenter";
}
return "redirect:/error";
}
// 论坛版块管理中心
@RequestMapping(value = "/manageBoard")
public String manageBoard(HttpServletRequest request) {
if (request.getSession().getAttribute("username").equals("admin")) {
List<Board> boards = boardService.listAllBoard();
request.setAttribute("boards", boards);
return "admin/manageBoard";
}
return "redirect:/error";
}
// 添加论坛板块
@RequestMapping(value = "addBoard", method = RequestMethod.POST)
public String addBoard(Board board, HttpServletRequest request) {
if (board != null) {
boardService.addBoardByBoard(board);
return "redirect:/admin/manageBoard";
}
request.getSession().setAttribute("Msg", "添加板块出错!");
return "admin/error";
}
// 修改板块信息
@RequestMapping(value = "updateBoard", method = RequestMethod.POST)
public String updateBoard(Board board, HttpServletRequest request) {
if (board != null) {
boardService.updateBoardInfo(board);
// 成功更新数据后跳转页面
return "redirect:/admin/manageBoard";
}
// 更新数据失败跳转到错误页面
return "redirect:/error";
}
// 管理用户信息
@RequestMapping(value = "manageUser")
public String manageUser(HttpServletRequest request) {
List<User> users = userService.getAllUser();
if (users != null) {
request.setAttribute("users", users);
return "/admin/allUserInfo";
}
return "redirect:/error";
}
// 管理发表的主题
@RequestMapping(value = "managePost")
public String managePost(HttpServletRequest request) {
List<Post> posts = postService.listAllPost();
if (posts != null) {
request.setAttribute("posts", posts);
return "/admin/allPostInfo";
}
return "redirect:/error";
}
// 删除已经发表的主题
@RequestMapping(value = "deletePost")
public String deletePost(int postId, int postBoardId) {
postService.deletePost(postId);
return "redirect:/board/listPosts-" + postBoardId;
}
// 删除回复
@RequestMapping(value = "deleteReply")
public String deleteReply(int replyId, int replyPostId) {
replyService.deleteReply(replyId);
return "redirect:/post/postContent-" + replyPostId;
}
// 删除板块
@RequestMapping(value = "deleteBoard")
public String deleteBoard(int boardId) {
boardService.deleteBoard(boardId);
return "redirect:manageBoard";
}
}
回复管理控制层:
@Controller
@RequestMapping(value = "/post")
public class PostController {
@Autowired
PostService postService;
@Autowired
BoardService boardService;
@Autowired
ReplyService replyService;
// 添加帖子
@RequestMapping(value = "/addPost")
public String addPost(Post post) {
if (post != null) {
Post newPost = post;
Timestamp createLoginTime = new Timestamp(new Date().getTime());
newPost.setPostCreateTime(createLoginTime);
newPost.setPostUpdateTime(createLoginTime);
postService.addPostByPost(newPost);
boardService.updatePostNum(newPost.getPostBoardId());
return "redirect:postContent-" + post.getPostId();
}
return "error";
}
// 查看帖子
@RequestMapping(value = "postContent-{postId}")
public String intoPost(@PathVariable int postId, HttpServletRequest request) {
System.out.println(postId);
Post post = postService.listPostContent(postId);
List<Reply> replies = replyService.listReplyByPostId(postId);
if (post == null) {
return "/error";
}
// 帖子有回复则添加回复信息
if (replies != null) {
request.setAttribute("replies", replies);
}
request.setAttribute("post", post);
return "post/postContent";
}
}