源码获取:俺的博客首页 "资源" 里下载!
管理员角色包含以下功能:
管理员登陆,用户管理,新闻管理,留言列表查看等功能。
用户角色包含以下功能:
查看所有新闻,市场资讯查看,科技推广查看,致富经栏目查看,网站留言,用户注册登陆等功能。
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.数据库:MySql 5.7版本;
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:HTML+CSS+JavaScript+jsp
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/ 登录
@Controller
@RequestMapping("/admin")
public class BlogController {
@Resource
private BlogService blogService;
@Resource
private CategoryService categoryService;
/**
* 博客列表
*
* @param params 参数
* @return com.hbu.myblog.util.Result
*/
@GetMapping("/blogs/list")
@ResponseBody
public Result list(@RequestParam Map params) {
if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
return ResultGenerator.genFailResult("参数异常!");
}
PageQueryUtil pageUtil = new PageQueryUtil(params);
return ResultGenerator.genSuccessResult(blogService.getBlogsPage(pageUtil));
}
/**
* @param request http请求
* @return java.lang.String
*/
@GetMapping("/blogs")
public String list(HttpServletRequest request) {
request.setAttribute("path", "blogs");
return "admin/blog";
}
/**
* @param request http请求
* @return java.lang.String
*/
@GetMapping("/blogs/edit")
public String edit(HttpServletRequest request) {
request.setAttribute("path", "edit");
request.setAttribute("categories", categoryService.getAllCategories());
return "admin/edit";
}
/**
* @param request http请求
* @param blogId 博客id
* @return java.lang.String
*/
@GetMapping("/blogs/edit/{blogId}")
public String edit(HttpServletRequest request, @PathVariable("blogId") Long blogId) {
request.setAttribute("path", "edit");
Blog blog = blogService.getBlogById(blogId);
if (blog == null) {
return "error/error_400";
}
request.setAttribute("blog", blog);
request.setAttribute("categories", categoryService.getAllCategories());
return "admin/edit";
}
/**
* 添加文章
*
* @param blogTitle 文章标题
* @param blogSummary 摘要
* @param blogCategoryId 类别
* @param blogTags 标签
* @param blogContent 内容
* @param blogStatus 草稿,发布
* @param enableComment 可否评论
* @return com.hbu.myblog.util.Result
*/
@PostMapping("/blogs/save")
@ResponseBody
public Result save(@RequestParam("blogTitle") String blogTitle,
@RequestParam(name = "blogSummary", required = false) String blogSummary,
@RequestParam("blogCategoryId") Integer blogCategoryId,
@RequestParam("blogTags") String blogTags,
@RequestParam("blogContent") String blogContent,
@RequestParam("blogStatus") Byte blogStatus,
@RequestParam("enableComment") Byte enableComment) {
if (StringUtils.isEmpty(blogTitle)) {
return ResultGenerator.genFailResult("请输入文章标题");
}
if (blogTitle.trim().length() > 150) {
return ResultGenerator.genFailResult("标题过长");
}
if (StringUtils.isEmpty(blogTags)) {
return ResultGenerator.genFailResult("请输入文章标签");
}
if (blogTags.trim().length() > 150) {
return ResultGenerator.genFailResult("标签过长");
}
if (blogSummary.trim().length() > 375) {
return ResultGenerator.genFailResult("摘要过长");
}
if (StringUtils.isEmpty(blogContent)) {
return ResultGenerator.genFailResult("请输入文章内容");
}
if (blogTags.trim().length() > 100000) {
return ResultGenerator.genFailResult("文章内容过长");
}
Blog blog = new Blog();
blog.setBlogTitle(blogTitle);
blog.setBlogSummary(blogSummary);
blog.setBlogCategoryId(blogCategoryId);
blog.setBlogTags(blogTags);
blog.setBlogContent(blogContent);
blog.setBlogStatus(blogStatus);
blog.setEnableComment(enableComment);
String saveBlogResult = blogService.saveBlog(blog);
if ("success".equals(saveBlogResult)) {
return ResultGenerator.genSuccessResult("添加成功");
} else {
return ResultGenerator.genFailResult(saveBlogResult);
}
}
/**
* 修改文章
*
* @param blogId 文章ID
* @param blogTitle 文章标题
* @param blogSummary 摘要
* @param blogCategoryId 类别
* @param blogTags 标签
* @param blogContent 内容
* @param blogStatus 草稿,发布
* @param enableComment 可否评论
* @return com.hbu.myblog.util.Result
*/
@PostMapping("/blogs/update")
@ResponseBody
public Result update(@RequestParam("blogId") Long blogId,
@RequestParam("blogTitle") String blogTitle,
@RequestParam(name = "blogSummary", required = false) String blogSummary,
@RequestParam("blogCategoryId") Integer blogCategoryId,
@RequestParam("blogTags") String blogTags,
@RequestParam("blogContent") String blogContent,
@RequestParam("blogStatus") Byte blogStatus,
@RequestParam("enableComment") Byte enableComment) {
if (StringUtils.isEmpty(blogTitle)) {
return ResultGenerator.genFailResult("请输入文章标题");
}
if (blogTitle.trim().length() > 150) {
return ResultGenerator.genFailResult("标题过长");
}
if (StringUtils.isEmpty(blogTags)) {
return ResultGenerator.genFailResult("请输入文章标签");
}
if (blogTags.trim().length() > 150) {
return ResultGenerator.genFailResult("标签过长");
}
if (blogSummary.trim().length() > 375) {
return ResultGenerator.genFailResult("摘要过长");
}
if (StringUtils.isEmpty(blogContent)) {
return ResultGenerator.genFailResult("请输入文章内容");
}
if (blogTags.trim().length() > 100000) {
return ResultGenerator.genFailResult("文章内容过长");
}
Blog blog = new Blog();
blog.setBlogId(blogId);
blog.setBlogTitle(blogTitle);
blog.setBlogSummary(blogSummary);
blog.setBlogCategoryId(blogCategoryId);
blog.setBlogTags(blogTags);
blog.setBlogContent(blogContent);
blog.setBlogStatus(blogStatus);
blog.setEnableComment(enableComment);
String updateBlogResult = blogService.updateBlog(blog);
if ("success".equals(updateBlogResult)) {
return ResultGenerator.genSuccessResult("修改成功");
} else {
return ResultGenerator.genFailResult(updateBlogResult);
}
}
/**
* 根据id的之删除文章
*
* @param ids 要删除文章id列表
* @return com.hbu.myblog.util.Result
*/
@PostMapping("/blogs/delete")
@ResponseBody
public Result delete(@RequestBody Integer[] ids) {
if (ids.length < 1) {
return ResultGenerator.genFailResult("参数异常!");
}
if (blogService.deleteBatch(ids)) {
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult("删除失败");
}
}
}
@Controller
@RequestMapping("/admin")
public class AdminController {
@Resource
private AdminUserService adminUserService;
@Resource
private BlogService blogService;
@Resource
private CategoryService categoryService;
@Resource
private TagService tagService;
@Resource
private CommentService commentService;
/**
* 处理登录请求
*
* @return java.lang.String
*/
@GetMapping({"/login"})
public String login() {
return "admin/login";
}
/**
* 主页
*
* @param request http请求
* @return java.lang.String
*/
@GetMapping({"", "/", "/index", "/index.html"})
public String index(HttpServletRequest request) {
request.setAttribute("path", "index");
request.setAttribute("categoryCount", categoryService.getTotalCategories());
request.setAttribute("blogCount", blogService.getTotalBlogs());
request.setAttribute("tagCount", tagService.getTotalTags());
request.setAttribute("commentCount", commentService.getTotalComments());
return "admin/index";
}
/**
* 登录界面
*
* @param userName 用户名
* @param password 密码
* @param verifyCode 验证码
* @param session session
* @return java.lang.String
*/
@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", "登陆失败");
return "admin/login";
}
}
/**
* 修改个人信息
*
* @param request http请求
* @return java.lang.String
*/
@GetMapping("/profile")
public String profile(HttpServletRequest request) {
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
if (adminUser == null) {
return "admin/login";
}
request.setAttribute("path", "profile");
request.setAttribute("loginUserName", adminUser.getLoginUserName());
request.setAttribute("nickName", adminUser.getNickName());
return "admin/profile";
}
/**
* 修改密码
*
* @param request http请求
* @param originalPassword 原始密码
* @param newPassword 新密码
* @return java.lang.String
*/
@PostMapping("/profile/password")
@ResponseBody
public String passwordUpdate(HttpServletRequest request, @RequestParam("originalPassword") String originalPassword,
@RequestParam("newPassword") String newPassword) {
if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)) {
return "参数不能为空";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updatePassword(loginUserId, originalPassword, newPassword)) {
//修改成功后清空session中的数据,前端控制跳转至登录页
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "success";
} else {
return "修改失败";
}
}
/**
* 修改登录名,昵称
*
* @param request http请求
* @param loginUserName 登录名
* @param nickName 昵称
* @return java.lang.String
*/
@PostMapping("/profile/name")
@ResponseBody
public String nameUpdate(HttpServletRequest request, @RequestParam("loginUserName") String loginUserName,
@RequestParam("nickName") String nickName) {
if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
return "参数不能为空";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
return "success";
} else {
return "修改失败";
}
}
/**
* 管理员退出
*
* @param request http请求
* @return java.lang.String
*/
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "admin/login";
}
}
@Controller
public class MyblogController {
// public static String theme = "default";
public static String theme = "amaze";
@Resource
private BlogService blogService;
@Resource
private TagService tagService;
@Resource
private CommentService commentService;
@Resource
private ConfigService configService;
@Resource
private CategoryService categoryService;
/**
* 首页
*
* @param request http请求
* @return java.lang.String
*/
@GetMapping({"/", "/index", "index.html"})
public String index(HttpServletRequest request) {
return this.page(request, 1);
}
/**
* 首页(带页码)
*
* @param request http请求
* @param pageNum 页码
* @return java.lang.String
*/
@GetMapping({"/page/{pageNum}"})
public String page(HttpServletRequest request, @PathVariable("pageNum") int pageNum) {
PageResult blogPageResult = blogService.getBlogsForIndexPage(pageNum);
if (blogPageResult == null) {
return "error/error_404";
}
request.setAttribute("blogPageResult", blogPageResult);
request.setAttribute("newBlogs", blogService.getBlogListForIndexPage(1));
request.setAttribute("hotBlogs", blogService.getBlogListForIndexPage(0));
request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
request.setAttribute("pageName", "首页");
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/index";
}
/**
* Categories页面(包括分类数据和标签数据)
*
* @param request http请求
* @return java.lang.String
*/
@GetMapping({"/categories"})
public String categories(HttpServletRequest request) {
request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
request.setAttribute("categories", categoryService.getAllCategories());
request.setAttribute("pageName", "分类页面");
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/category";
}
/**
* 详情页
*
* @param request http请求
* @param blogId 博客id
* @param commentPage 评论页
* @return java.lang.String
*/
@GetMapping({"/blog/{blogId}", "/article/{blogId}"})
public String detail(HttpServletRequest request, @PathVariable("blogId") Long blogId, @RequestParam(value = "commentPage", required = false, defaultValue = "1") Integer commentPage) {
BlogDetailVO blogDetailVO = blogService.getBlogDetail(blogId);
if (blogDetailVO != null) {
request.setAttribute("blogDetailVO", blogDetailVO);
request.setAttribute("commentPageResult", commentService.getCommentPageByBlogIdAndPageNum(blogId, commentPage));
}
request.setAttribute("pageName", "详情");
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/detail";
}
/**
* 标签列表页
*
* @param request http请求
* @param tagName 标签名称
* @return java.lang.String
*/
@GetMapping({"/tag/{tagName}"})
public String tag(HttpServletRequest request, @PathVariable("tagName") String tagName) {
return tag(request, tagName, 1);
}
/**
* 标签列表页(带页码)
*
* @param request http请求
* @param tagName 标签名称
* @param page 页码
* @return java.lang.String
*/
@GetMapping({"/tag/{tagName}/{page}"})
public String tag(HttpServletRequest request, @PathVariable("tagName") String tagName, @PathVariable("page") Integer page) {
PageResult blogPageResult = blogService.getBlogsPageByTag(tagName, page);
request.setAttribute("blogPageResult", blogPageResult);
request.setAttribute("pageName", "标签");
request.setAttribute("pageUrl", "tag");
request.setAttribute("keyword", tagName);
request.setAttribute("newBlogs", blogService.getBlogListForIndexPage(1));
request.setAttribute("hotBlogs", blogService.getBlogListForIndexPage(0));
request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/list";
}
/**
* 分类列表页
*
* @param request http请求
* @param categoryName 类别名称
* @return java.lang.String
*/
@GetMapping({"/category/{categoryName}"})
public String category(HttpServletRequest request, @PathVariable("categoryName") String categoryName) {
return category(request, categoryName, 1);
}
/**
* 分类列表页(带页码)
*
* @param request http请求
* @param categoryName 类别名称
* @param page 页码
* @return java.lang.String
*/
@GetMapping({"/category/{categoryName}/{page}"})
public String category(HttpServletRequest request, @PathVariable("categoryName") String categoryName, @PathVariable("page") Integer page) {
PageResult blogPageResult = blogService.getBlogsPageByCategory(categoryName, page);
request.setAttribute("blogPageResult", blogPageResult);
request.setAttribute("pageName", "分类");
request.setAttribute("pageUrl", "category");
request.setAttribute("keyword", categoryName);
request.setAttribute("newBlogs", blogService.getBlogListForIndexPage(1));
request.setAttribute("hotBlogs", blogService.getBlogListForIndexPage(0));
request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/list";
}
/**
* 搜索列表页
*
* @param request http请求
* @param keyword 关键词
* @return java.lang.String
*/
@GetMapping({"/search/{keyword}"})
public String search(HttpServletRequest request, @PathVariable("keyword") String keyword) {
return search(request, keyword, 1);
}
/**
* 搜索列表页(带页码)
*
* @param request http请求
* @param keyword 关键词
* @param page 页码
* @return java.lang.String
*/
@GetMapping({"/search/{keyword}/{page}"})
public String search(HttpServletRequest request, @PathVariable("keyword") String keyword, @PathVariable("page") Integer page) {
PageResult blogPageResult = blogService.getBlogsPageBySearch(keyword, page);
request.setAttribute("blogPageResult", blogPageResult);
request.setAttribute("pageName", "搜索");
request.setAttribute("pageUrl", "search");
request.setAttribute("keyword", keyword);
request.setAttribute("newBlogs", blogService.getBlogListForIndexPage(1));
request.setAttribute("hotBlogs", blogService.getBlogListForIndexPage(0));
request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
request.setAttribute("configurations", configService.getAllConfigs());
return "blog/" + theme + "/list";
}
/**
* 评论留言
*
* @param request http请求
* @param session session
* @param blogId 博客id
* @param verifyCode 验证码
* @param commentator 评论者昵称
* @param email 邮箱
* @param websiteUrl 留言者的网站
* @param commentBody 评论内容
* @return com.hbu.myblog.util.Result
*/
@PostMapping(value = "/blog/comment")
@ResponseBody
public Result comment(HttpServletRequest request, HttpSession session,
@RequestParam Long blogId, @RequestParam String verifyCode,
@RequestParam String commentator, @RequestParam String email,
@RequestParam String websiteUrl, @RequestParam String commentBody) {
if (StringUtils.isEmpty(verifyCode)) {
return ResultGenerator.genFailResult("验证码不能为空");
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode)) {
return ResultGenerator.genFailResult("非法请求");
}
if (!verifyCode.equals(kaptchaCode)) {
return ResultGenerator.genFailResult("验证码错误");
}
String ref = request.getHeader("Referer");
if (StringUtils.isEmpty(ref)) {
return ResultGenerator.genFailResult("非法请求");
}
if (null == blogId || blogId < 0) {
return ResultGenerator.genFailResult("非法请求");
}
if (StringUtils.isEmpty(commentator)) {
return ResultGenerator.genFailResult("请输入称呼");
}
if (StringUtils.isEmpty(email)) {
return ResultGenerator.genFailResult("请输入邮箱地址");
}
if (!PatternUtil.isEmail(email)) {
return ResultGenerator.genFailResult("请输入正确的邮箱地址");
}
if (StringUtils.isEmpty(commentBody)) {
return ResultGenerator.genFailResult("请输入评论内容");
}
if (commentBody.trim().length() > 200) {
return ResultGenerator.genFailResult("评论内容过长");
}
BlogComment comment = new BlogComment();
comment.setBlogId(blogId);
comment.setCommentator(MyBlogUtils.cleanString(commentator));
comment.setEmail(email);
if (PatternUtil.isURL(websiteUrl)) {
comment.setWebsiteUrl(websiteUrl);
}
comment.setCommentBody(MyBlogUtils.cleanString(commentBody));
return ResultGenerator.genSuccessResult(commentService.addComment(comment));
}
}
源码获取:俺的博客首页 "资源" 里下载!