current position:Home>Java project: simple personal blog system (java + SSM + MySQL + JSP)
Java project: simple personal blog system (java + SSM + MySQL + JSP)
2022-01-27 05:07:20 【qq_ one billion three hundred and thirty-four million six hundr】
Spring+SpringMVC+Mybatis Implement a simple personal blog system :
The front desk is only responsible for checking blogs and comments , backstage : Article management 、 Comment management 、 Personal center 、 Change Password 、 Clear cache, etc
The operating environment is as follows : jdk1.8 Tomcat8.5/7.0 maven3.5/3.6 Mysql5.7 IDEA Windows/linux
Category control layer :
/**
* Category control layer
*/
@Controller
@RequestMapping("/admin")
public class CategoryController {
@Resource
private CategoryService categoryService;
/**
* @param request
* @return java.lang.String
*/
@GetMapping("/categories")
public String categoryPage(HttpServletRequest request) {
request.setAttribute("path", "categories");
return "admin/category";
}
/**
* @param params
* @return com.hbu.myblog.util.Result
*/
@RequestMapping(value = "/categories/list", method = RequestMethod.GET)
@ResponseBody
public Result list(@RequestParam Map<String, Object> params) {
if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
return ResultGenerator.genFailResult(" Parameter exception !");
}
PageQueryUtil pageUtil = new PageQueryUtil(params);
return ResultGenerator.genSuccessResult(categoryService.getBlogCategoryPage(pageUtil));
}
/**
* @param categoryName
* @param categoryIcon
* @return com.hbu.myblog.util.Result
*/
@RequestMapping(value = "/categories/save", method = RequestMethod.POST)
@ResponseBody
public Result save(@RequestParam("categoryName") String categoryName,
@RequestParam("categoryIcon") String categoryIcon) {
if (StringUtils.isEmpty(categoryName)) {
return ResultGenerator.genFailResult(" Please enter the classification name !");
}
if (StringUtils.isEmpty(categoryIcon)) {
return ResultGenerator.genFailResult(" Please select the category icon !");
}
if (categoryService.saveCategory(categoryName, categoryIcon)) {
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult(" Duplicate category name ");
}
}
/**
* @param categoryId
* @param categoryName
* @param categoryIcon
* @return com.hbu.myblog.util.Result
*/
@RequestMapping(value = "/categories/update", method = RequestMethod.POST)
@ResponseBody
public Result update(@RequestParam("categoryId") Integer categoryId,
@RequestParam("categoryName") String categoryName,
@RequestParam("categoryIcon") String categoryIcon) {
if (StringUtils.isEmpty(categoryName)) {
return ResultGenerator.genFailResult(" Please enter the classification name !");
}
if (StringUtils.isEmpty(categoryIcon)) {
return ResultGenerator.genFailResult(" Please select the category icon !");
}
if (categoryService.updateCategory(categoryId, categoryName, categoryIcon)) {
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult(" Duplicate category name ");
}
}
/**
* @param ids
* @return com.hbu.myblog.util.Result
*/
@RequestMapping(value = "/categories/delete", method = RequestMethod.POST)
@ResponseBody
public Result delete(@RequestBody Integer[] ids) {
if (ids.length < 1) {
return ResultGenerator.genFailResult(" Parameter exception !");
}
if (categoryService.deleteBatch(ids)) {
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult(" Delete failed ");
}
}
}
Handle administrator interface requests :
/**
* Handle administrator interface requests
*
*/
@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;
/**
* Processing login requests
*
* @return java.lang.String
*/
@GetMapping({"/login"})
public String login() {
return "admin/login";
}
/**
* Home page
*
* @param request http request
* @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";
}
/**
* Login screen
*
* @param userName user name
* @param password password
* @param verifyCode Verification Code
* @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", " Verification code cannot be empty ");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", " User name or password cannot be empty ");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", " Verification code error ");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session The expiration time is set to 7200 second That's two hours
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", " Login failed ");
return "admin/login";
}
}
/**
* Modify personal information
*
* @param request http request
* @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";
}
/**
* Change Password
*
* @param request http request
* @param originalPassword The original password
* @param newPassword New password
* @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 " The parameter cannot be null ";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updatePassword(loginUserId, originalPassword, newPassword)) {
// Empty after modification session Data in , The front control jumps to the login page
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "success";
} else {
return " Modification failed ";
}
}
/**
* Change the login name , nickname
*
* @param request http request
* @param loginUserName Login name
* @param nickName 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 " The parameter cannot be null ";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
return "success";
} else {
return " Modification failed ";
}
}
/**
* Administrator exit
*
* @param request http request
* @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";
}
}
Blog control layer :
/**
* Blog control layer
*/
@Controller
@RequestMapping("/admin")
public class BlogController {
@Resource
private BlogService blogService;
@Resource
private CategoryService categoryService;
/**
* Blog list
*
* @param params Parameters
* @return com.hbu.myblog.util.Result
*/
@GetMapping("/blogs/list")
@ResponseBody
public Result list(@RequestParam Map<String, Object> params) {
if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
return ResultGenerator.genFailResult(" Parameter exception !");
}
PageQueryUtil pageUtil = new PageQueryUtil(params);
return ResultGenerator.genSuccessResult(blogService.getBlogsPage(pageUtil));
}
/**
* @param request http request
* @return java.lang.String
*/
@GetMapping("/blogs")
public String list(HttpServletRequest request) {
request.setAttribute("path", "blogs");
return "admin/blog";
}
/**
* @param request http request
* @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 request
* @param blogId Blog 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";
}
/**
* Add the article
*
* @param blogTitle Article title
* @param blogSummary Abstract
* @param blogCategoryId Category
* @param blogTags label
* @param blogContent Content
* @param blogStatus draft , Release
* @param enableComment Can you comment
* @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(" Please enter the title of the article ");
}
if (blogTitle.trim().length() > 150) {
return ResultGenerator.genFailResult(" The title is too long ");
}
if (StringUtils.isEmpty(blogTags)) {
return ResultGenerator.genFailResult(" Please enter the article label ");
}
if (blogTags.trim().length() > 150) {
return ResultGenerator.genFailResult(" The label is too long ");
}
if (blogSummary.trim().length() > 375) {
return ResultGenerator.genFailResult(" The summary is too long ");
}
if (StringUtils.isEmpty(blogContent)) {
return ResultGenerator.genFailResult(" Please enter the content of the article ");
}
if (blogTags.trim().length() > 100000) {
return ResultGenerator.genFailResult(" The content of the article is too long ");
}
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(" Add success ");
} else {
return ResultGenerator.genFailResult(saveBlogResult);
}
}
/**
* Revise article
*
* @param blogId article ID
* @param blogTitle Article title
* @param blogSummary Abstract
* @param blogCategoryId Category
* @param blogTags label
* @param blogContent Content
* @param blogStatus draft , Release
* @param enableComment Can you comment
* @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(" Please enter the title of the article ");
}
if (blogTitle.trim().length() > 150) {
return ResultGenerator.genFailResult(" The title is too long ");
}
if (StringUtils.isEmpty(blogTags)) {
return ResultGenerator.genFailResult(" Please enter the article label ");
}
if (blogTags.trim().length() > 150) {
return ResultGenerator.genFailResult(" The label is too long ");
}
if (blogSummary.trim().length() > 375) {
return ResultGenerator.genFailResult(" The summary is too long ");
}
if (StringUtils.isEmpty(blogContent)) {
return ResultGenerator.genFailResult(" Please enter the content of the article ");
}
if (blogTags.trim().length() > 100000) {
return ResultGenerator.genFailResult(" The content of the article is too long ");
}
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(" Modification successful ");
} else {
return ResultGenerator.genFailResult(updateBlogResult);
}
}
/**
* according to id Delete the article
*
* @param ids To delete an article id list
* @return com.hbu.myblog.util.Result
*/
@PostMapping("/blogs/delete")
@ResponseBody
public Result delete(@RequestBody Integer[] ids) {
if (ids.length < 1) {
return ResultGenerator.genFailResult(" Parameter exception !");
}
if (blogService.deleteBatch(ids)) {
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult(" Delete failed ");
}
}
}
copyright notice
author[qq_ one billion three hundred and thirty-four million six hundr],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270507188294.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command