当前位置:   article > 正文

python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-自定义帖子管理实现

python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-自定义帖子管理实现

锋哥原创的Springboot+Layui python222网站实战:

python222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )_哔哩哔哩_bilibilipython222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )共计23条视频,包括:python222网站实战课程视频教程(SpringBoot+Python爬虫实战) ( 火爆连载更新中... )、第2讲 架构搭建实现、第3讲 页面系统属性动态化设计实现等,UP主更多精彩视频,请关注UP账号。icon-default.png?t=N7T8https://www.bilibili.com/video/BV1yX4y1a7qM/

我们有些文章,比如自我介绍帖子,vip课程帖子,这种区别于正常发文文章的帖子,我们可以单独管理,设计成自定义帖子模块。

新建t_post表

  1. create table `t_post` (
  2. `id` int (11),
  3. `title` varchar (600),
  4. `content` text
  5. );
  6. insert into `t_post` (`id`, `title`, `content`) values('2','2',NULL);
  7. insert into `t_post` (`id`, `title`, `content`) values('3','3','<p>222</p>\n');
  8. insert into `t_post` (`id`, `title`, `content`) values('5','4测试444','<p>32324444<img alt=\"\" src=\"/articleImages/20230615120852.jpg\" style=\"height:1372px; width:720px\" /></p>\n');

用Mybatis-X生成代码;

新建PostAdminController类:

  1. package com.python222.controller.admin;
  2. import com.python222.entity.Post;
  3. import com.python222.service.PostService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * 管理员-自定义帖子控制器
  12. * @author python222小锋老师
  13. * @site www.python222.com
  14. */
  15. @RestController
  16. @RequestMapping(value = "/admin/post")
  17. public class PostAdminController {
  18. @Autowired
  19. private PostService postService;
  20. /**
  21. * 根据条件查询自定义帖子
  22. * @return
  23. * @throws Exception
  24. */
  25. @RequestMapping(value = "/list")
  26. public Map<String,Object> list()throws Exception{
  27. Map<String, Object> resultMap = new HashMap<>();
  28. List<Post> postList=postService.list();
  29. resultMap.put("code", 0);
  30. resultMap.put("data", postList);
  31. return resultMap;
  32. }
  33. /**
  34. * 添加或者修改自定义帖子
  35. * @param post
  36. * @return
  37. */
  38. @RequestMapping("/save")
  39. public Map<String,Object> save(Post post){
  40. if(post.getId()==null){
  41. postService.save(post);
  42. }else{
  43. postService.updateById(post);
  44. }
  45. Map<String, Object> resultMap = new HashMap<>();
  46. resultMap.put("success", true);
  47. return resultMap;
  48. }
  49. /**
  50. * 删除自定义帖子
  51. * @param id
  52. * @return
  53. * @throws Exception
  54. */
  55. @RequestMapping("/delete")
  56. public Map<String,Object> delete(Integer id)throws Exception{
  57. Map<String, Object> resultMap = new HashMap<>();
  58. postService.removeById(id);
  59. resultMap.put("success", true);
  60. return resultMap;
  61. }
  62. /**
  63. * 根据id查询自定义帖子实体
  64. * @param id
  65. * @return
  66. * @throws Exception
  67. */
  68. @RequestMapping("/findById")
  69. public Map<String,Object> findById(Integer id)throws Exception{
  70. Map<String, Object> resultMap = new HashMap<>();
  71. Post post=postService.getById(id);
  72. resultMap.put("post", post);
  73. resultMap.put("success", true);
  74. return resultMap;
  75. }
  76. }

前端postManage.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>自定义帖子管理</title>
  6. <link rel="stylesheet" href="/static/layui/css/layui.css"></link>
  7. <link rel="stylesheet" href="/static/css/css.css"></link>
  8. </head>
  9. <body>
  10. <div style="padding: 20px">
  11. <span class="layui-breadcrumb">
  12. <a>首页</a>
  13. <a><cite>自定义帖子管理</cite></a>
  14. </span>
  15. <div style="padding-top: 20px;">
  16. <div>
  17. <div>
  18. <button class="layui-btn layuiadmin-btn-list" data-type="batchdel" onclick="addPost()">添加</button>
  19. </div>
  20. </div>
  21. <div>
  22. <table width="100%" id="postListTable" ></table>
  23. </div>
  24. </div>
  25. </div>
  26. <script src="/static/layui/layui.js"></script>
  27. <script src="/static/js/jquery.js"></script>
  28. <script src="/static/js/common.js"></script>
  29. <script type="text/javascript">
  30. layui.use(['element','form','table'], function(){
  31. var form=layui.form;
  32. var element = layui.element; //导航的hover效果、二级菜单等功能,需要依赖element模块
  33. $ = layui.jquery; // 使用jquery
  34. table = layui.table;
  35. table.render({
  36. elem: '#postListTable'
  37. ,url:'/admin/post/list'
  38. ,cols: [[
  39. {type:'checkbox'}
  40. ,{field:'id', width:100,title: '编号'}
  41. ,{field:'title', title: '自定义帖子名称'}
  42. ,{field:'action', width:300, title: '操作',align:'center',templet:formatAction}
  43. ]]
  44. ,page: true
  45. });
  46. });
  47. function deleteOne(id){
  48. layer.confirm('您确定要删除这条记录吗?', {
  49. title:"系统提示"
  50. ,btn: ['确定','取消'] //按钮
  51. }, function(){
  52. layer.closeAll('dialog');
  53. $.post("/admin/post/delete",{"id":id},function(result){
  54. if(result.success){
  55. layer.msg("删除成功!");
  56. table.reload("postListTable",{});
  57. }else{
  58. layer.msg("删除失败,请联系管理员!");
  59. }
  60. },"json");
  61. }, function(){
  62. });
  63. }
  64. function addPost(){
  65. layer.open({
  66. type: 2,
  67. title: '添加自定义帖子',
  68. area: ['1000px', '730px'],
  69. content: '/admin/savePost.html' //iframe的url
  70. });
  71. }
  72. function modifyPost(id){
  73. layer.open({
  74. type: 2,
  75. title: '修改自定义帖子',
  76. area: ['1000px', '730px'],
  77. content: '/admin/savePost.html?id='+id //iframe的url
  78. });
  79. }
  80. function formatAction(d){
  81. return "<a class='layui-btn layui-btn-danger layui-btn-xs' target='_blank' href='/post/"+d.id+"'><i class='layui-icon layui-icon-read'></i>帖子预览</a><button class='layui-btn layui-btn-normal layui-btn-xs' onclick='modifyPost("+d.id+")'><i class='layui-icon layui-icon-edit'></i>编辑</button><button class='layui-btn layui-btn-warm layui-btn-xs' onclick='deleteOne("+d.id+")'><i class='layui-icon layui-icon-delete' ></i>删除</button>";
  82. }
  83. </script>
  84. </body>
  85. </html>

再新建一个savePost.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>添加或者修改自定义帖子</title>
  6. <link rel="stylesheet" href="/static/layui/css/layui.css"></link>
  7. <style type="text/css">
  8. table tr td{
  9. padding: 10px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div style="padding: 20px">
  15. <form method="post">
  16. <table>
  17. <tr>
  18. <td>标题:</td>
  19. <td><input type="text" autocomplete="off" id="title" name="title" class="layui-input" style="width: 360px"/></td>
  20. </tr>
  21. <tr>
  22. <td valign="top">内容:</td>
  23. <td>
  24. <textarea type="text" class="ckeditor" id="content" name="content" class="layui-input" ></textarea>
  25. </td>
  26. </tr>
  27. <tr>
  28. <td><button class="layui-btn" onclick="submitData();return false;">提交</button></td>
  29. <td><font id="errorInfo" color="red"></font></td>
  30. </tr>
  31. </table>
  32. </form>
  33. </div>
  34. <script src="/static/ckeditor/ckeditor.js"></script>
  35. <script src="/static/layui/layui.js"></script>
  36. <script src="/static/js/jquery.js"></script>
  37. <script src="/static/js/common.js"></script>
  38. <script type="text/javascript">
  39. layui.use(['form'], function(){
  40. });
  41. function submitData(){
  42. var title=$("#title").val().trim();
  43. var content=CKEDITOR.instances.content.getData();
  44. if(title=="") {
  45. $("#errorInfo").text("请输入自定义帖子标题!");
  46. $("#name").focus();
  47. return false;
  48. }
  49. if(content=="") {
  50. $("#errorInfo").text("请输入自定义帖子内容!");
  51. $("#url").focus();
  52. return false;
  53. }
  54. var id=getQueryVariable("id");
  55. if(id){
  56. $.post("/admin/post/save",{id:id,title:title,content:content},function(result){
  57. if(result.success){
  58. layer.alert('修改成功!',function () {
  59. parent.reloadPage();
  60. });
  61. }else{
  62. $("#errorInfo").text(result.errorInfo);
  63. }
  64. },"json");
  65. }else{
  66. $.post("/admin/post/save",{title:title,content:content},function(result){
  67. if(result.success){
  68. layer.alert('添加成功!',function () {
  69. parent.reloadPage();
  70. });
  71. }else{
  72. $("#errorInfo").text(result.errorInfo);
  73. }
  74. },"json");
  75. }
  76. }
  77. function getQueryVariable(variable){
  78. var query = window.location.search.substring(1);
  79. var vars = query.split("&");
  80. for (var i=0;i<vars.length;i++) {
  81. var pair = vars[i].split("=");
  82. if(pair[0] == variable){return pair[1];}
  83. }
  84. return(false);
  85. }
  86. $(function(){
  87. var id=getQueryVariable("id");
  88. if(id){
  89. $.post("/admin/post/findById",{id:id},function(result){
  90. if(result.success){
  91. var post=result.post;
  92. $("#title").val(post.title);
  93. CKEDITOR.instances.content.setData(post.content);
  94. }else{
  95. layer.alert("服务器加载有问题,请联系管理员!");
  96. }
  97. },"json");
  98. }
  99. });
  100. </script>
  101. </body>
  102. </html>

自定义帖子详情查看:

新建PostController类:

  1. package com.python222.controller;
  2. import com.python222.entity.Post;
  3. import com.python222.service.PostService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.servlet.ModelAndView;
  9. /**
  10. * 自定义帖子控制器
  11. * @author python222小锋老师
  12. * @site www.python222.com
  13. */
  14. @Controller
  15. @RequestMapping("/post")
  16. public class PostController {
  17. @Autowired
  18. private PostService postService;
  19. /**
  20. * 根据id查询自定义帖子详细信息
  21. * @param id
  22. * @return
  23. * @throws Exception
  24. */
  25. @RequestMapping("/{id}")
  26. public ModelAndView view(@PathVariable("id")Integer id)throws Exception{
  27. ModelAndView mav=new ModelAndView();
  28. Post post = postService.getById(id);
  29. mav.setViewName("post");
  30. mav.addObject("post",post);
  31. return mav;
  32. }
  33. }

前端新建模板post.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title th:text="${post.title}+'-'+${application.propertyMap['k1']}"></title>
  6. <meta name="keywords" th:content="${post.title}">
  7. <meta name="description" th:content="${post.title}">
  8. <link href="/systemImages/favicon.ico" rel="SHORTCUT ICON">
  9. <link rel="stylesheet" href="/static/layui/css/layui.css"/>
  10. <link rel="stylesheet" href="/static/css/css.css"/>
  11. <link rel="stylesheet" type="text/css" href="/static/css/normalize.css" />
  12. <link rel="stylesheet" href="/static/css/font-awesome.min.css">
  13. </head>
  14. <body>
  15. <div class="header_top" th:include="common/head::#h" />
  16. <div class="header" th:include="common/menu::#m" />
  17. <div class="main_wrapper">
  18. <div class="w1220 article_content">
  19. <div class="title" th:text="${post.title}"></div>
  20. <div class="content" th:utext="${post.content}"></div>
  21. </div>
  22. </div>
  23. <div class="footer" th:include="common/footer::#f" />
  24. </body>
  25. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49988
推荐阅读
相关标签
  

闽ICP备14008679号