实训-个人博客(博客管理)

本文详细介绍了如何实现个人博客的分页查询、新增、编辑和删除功能。从数据库连接到前后端交互,逐步讲解每个步骤,包括接口定义、服务实现、控制器更新以及页面模板的修改。通过这个实训,可以掌握博客系统的完整开发流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、博客分页查询
1,新建BlogService接口

package net.zl.myblog.service;


import net.zl.myblog.po.Blog;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;


/**
 *
 */
public interface BlogService {
    Blog getBlog(Long id);//根据id查询
    Page<Blog> listBlog(Pageable pageable, Blog blog);//分页查询
    Blog saveBlog(Blog blog);//新增
    Blog updateBlog(Long id,Blog blog);//更新
    void deleteBlog(Long id);//删除
}

2,新建BlogRepository接口

package net.zl.myblog.dao;


import net.zl.myblog.po.Blog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;



import java.util.List;

/**
 * 
 */
public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {
}

3,新建BlogServiceImpl.java类

package net.zl.myblog.service;


import net.zl.myblog.NotFoundException;
import net.zl.myblog.dao.BlogRepository;
import net.zl.myblog.po.Blog;
import net.zl.myblog.po.Type;
import net.zl.myblog.util.MarkdownUtils;
import net.zl.myblog.util.MyBeanUtils;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

/**
 *
 */
@Service
public class BlogServiceImpl implements BlogService {


    @Autowired
    private BlogRepository blogRepository;

    //根据id查询
    @Override
    public Blog getBlog(Long id) {
        return blogRepository.findById(id).orElse(null);
    }

    @Transactional
    @Override
    public Blog getAndConvert(Long id) {
        Blog blog=blogRepository.findById(id).orElse(null);
        if(blog==null){
            throw new NotFoundException("该博客不存在");
        }
        Blog b=new Blog();
        BeanUtils.copyProperties(blog,b);
        String content=b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));

        blogRepository.updateViews(id);
        return b;
    }

    //分页动态查询
    @Override
    public Page<Blog> listBlog(Pageable pageable, BlogQuery blog) {
        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (!"".equals(blog.getTitle()) && blog.getTitle() != null) {
                    predicates.add(cb.like(root.<String>get("title"), "%"+blog.getTitle()+"%"));
                }
                if (blog.getTypeId() != null) {
                    predicates.add(cb.equal(root.<Type>get("type").get("id"), blog.getTypeId()));
                }
                if (blog.isRecommend()) {
                    predicates.add(cb.equal(root.<Boolean>get("recommend"), blog.isRecommend()));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable) {
        return blogRepository.findAll(pageable);
    }

    @Override
    public Page<Blog> listBlog(Long tagId, Pageable pageable) {
        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                Join join= root.join("tags");
                return cb.equal(join.get("id"),tagId);
            }
        },pageable);
    }

    @Override
    public Page<Blog> listBlog(String query, Pageable pageable) {
        return blogRepository.findByQuery(query,pageable);
    }

    @Override
    public List<Blog> listRecommendBlogTop(Integer size) {
        Sort sort=Sort.by(Sort.Direction.DESC,"updateTime");
        Pageable pageable = PageRequest.of(0, size, sort);
        return blogRepository.findTop(pageable);
    }

    @Override
    public Map<String, List<Blog>> archiveBlog() {
        List<String> years=blogRepository.findGroupYear();
        TreeMap<String,List<Blog>> map=new TreeMap<>((o1,o2)->Integer.parseInt(o2)-Integer.parseInt(o1));
//        Map<String, List<Blog>> map =new LinkedHashMap<>();
        for (String year : years){
            map.put(year,blogRepository.findByYear(year));
        }
        return map;
    }

    @Override
    public long countBlog() {
        return blogRepository.count();
    }

    //新增
    @Transactional
    @Override
    public Blog saveBlog(Blog blog) {
        if(blog.getId()==null){
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);//浏览次数
        }else {
            blog.setUpdateTime(new Date());
        }
        return blogRepository.save(blog);
    }
    //更新
    @Transactional
    @Override
    public Blog updateBlog(Long id, Blog blog) {
        Blog b=blogRepository.findById(id).orElse(null);
        if (b == null) {
            throw new NotFoundException("该博客不存在");
        }
        BeanUtils.copyProperties(blog,b, MyBeanUtils.getNullPropertyNames(blog));
        b.setUpdateTime(new Date());
        return blogRepository.save(b);
    }
    //删除
    @Transactional
    @Override
    public void deleteBlog(Long id) {
        blogRepository.deleteById(id);
    }


}

4、修改BlogController

package net.zl.myblog.web.admin;


import net.zl.myblog.po.Blog;
import net.zl.myblog.po.User;
import net.zl.myblog.service.BlogService;
import net.zl.myblog.service.TagService;
import net.zl.myblog.service.TypeService;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


/**
 * blogs控制器
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;


    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                Pageable pageable, Blog blog, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return “admin/blogs";
    }

}

5、修改blogs.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/me.css">
    <title>博客管理</title>
</head>
<body>
    <!--导航-->
    <nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
        <div class="ui container">
            <div class="ui inverted secondary stackable menu">
                <h2 class="ui teal header item">管理后台</h2>
                <a href="#" class="active m-item item m-mobile-hide"><i class="mini home icon"></i>博客</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini idea icon"></i>分类</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini tags icon"></i>标签</a>
                <div class="right m-item m-mobile-hide menu">
                    <div class="ui dropdown item">
                        <div class="text">
                            <img src="https://unsplash.it/100/100?image=1005" alt="" class="ui avatar image">
                            周璐
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="#" class="item">注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show"  >
            <i class="sidebar icon"></i>
        </a>
    </nav>
    <!--二级导航-->
    <div class="ui attached pointing menu">
        <div class="ui container">
            <div class="right menu">
                <a href="#" th:href="@{/admin/blogs/input}" class="item">发布</a>
                <a href="#" th:href="@{/admin/blogs}" class="teal active item">列表</a>
            </div>
        </div>
    </div>
    <!--中间内容-->
    <div class="m-container-small m-padded-tb-big">
        <div class="ui container">
            <!--中间头部表单-->
            <div  class="ui secondary segment form">
                <input type="hidden" name="page" >
                <div class="inline fields">
                    <!--标题-->
                    <div class="field">
                        <input type="text" name="title" placeholder="标题">
                    </div>
                    <!--分类-->
                    <div class="field">
                        <div class="ui labeled action input">
                            <div class="ui type selection dropdown">
                                <input type="hidden" name="type">
                                <i class="dropdown icon"></i>
                                <div class="default text">分类</div>
                                <div class="menu">
                                    <div th:each="type : ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">错误日志</div>
                                    <!--/*-->
                                    <div class="item" data-value="2">开发者手册</div>
                                    <!--*/-->
                                </div>
                            </div>
                            <button id="clear-btn" class="ui compact button">clear</button>
                        </div>
                    </div>
                    <!--推荐-->
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="recommend" name="recommend">
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <!--搜索-->
                    <div class="field">
                        <button type="button" id="search-btn" class="ui mini teal basic button"><i class="search icon"></i>搜索</button>
                    </div>
                </div>
            </div>
            <div id="table-container">
                <!--中间表格-->
                <table th:fragment="blogList" class="ui compact teal table">
                    <!--表头-->
                    <thead>
                    <tr>
                        <th></th>
                        <th>标题</th>
                        <th>类型</th>
                        <th>推荐</th>
                        <th>状态</th>
                        <th>更新时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <!--表格内容-->
                    <tbody>
                    <tr th:each="blog,iterStat : ${page.content}">
                        <td th:text="${iterStat.count}">1</td>
                        <td th:text="${blog.title}">刻意练习清单</td>
                        <td th:text="${blog.type.name}">认知升级</td>
                        <td th:text="${blog.recommend} ? '':''"></td>
                        <td th:text="${blog.published} ? '发布':'草稿'">草稿</td>
                        <td th:text="${blog.updateTime}">2017-10-02 09:45</td>
                        <td>
                            <a href="#" th:href="@{/admin/blogs/{id}/input(id=${blog.id})}" class="ui mini teal basic button">编辑</a>
                            <a href="#" th:href="@{/admin/blogs/{id}/delete(id=${blog.id})}" class="ui mini red basic button">删除</a>
                        </td>
                    </tr>
                    </tbody>
                    <!--表格底部-->
                    <tfoot>
                    <tr>
                        <th colspan="7">
                            <div class="ui mini pagination menu" th:if="${page.totalPages}>1" >
                                <a onclick="page(this)" th:attr="data-page=${page.number}-1" class="item" th:unless="${page.first}">上一页</a>
                                <a onclick="page(this)" th:attr="data-page=${page.number}+1" class=" item" th:unless="${page.last}">下一页</a>
                            </div>
                            <a href="#" th:href="@{/admin/blogs/input}" class="ui mini right floated teal basic button">新增</a>
                        </th>
                    </tr>
                    </tfoot>
                </table>

                <div class="ui success message" th:unless="${#strings.isEmpty(message)}">
                    <i class="close icon"></i>
                    <div class="header">提示:</div>
                    <p th:text="${message}">恭喜,操作成功!</p>
                </div>
            </div>
        </div>
    </div>
    <!--底部footer-->
    <footer th:replace="admin/_fragments :: footer" class="ui inverted  vertical segment m-padded-tb-massive">
        <div class="ui center aligned container">
            <div class="ui inverted divided stackable grid">
                <div class="three wide column">
                    <div class="ui inverted link list">
                        <div class="item">
                            <img src="../../static/images/wechat.jpg" class="ui rounded image" style="width: 110px">
                        </div>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">最新博客</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">联系我</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">Email:2392347073@qq.com</a>
                        <a href="#" class="item">QQ:2392347073</a>
                    </div>
                </div>
                <div class="seven wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">Lirenmi</h4>
                    <p class="">这是我的个人博客,会分享关于编程、写作、思考相关的任何内容,希望可以给来到这里的人有所帮助...</p>
                </div>
            </div>
            <div class="ui inverted section divider"></div>
            <p class="m-text-thin m-text-spaced m-opacity-tiny">Copyright  2016-2017 Lirenmi DESIGNED BYlIRENMI </p>
        </div>

    </footer>
    <!--/*/<th:block th:replace="admin/_fragments :: script">/*/-->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script>
    <!--/*/</th:block>/*/-->
    <script>
        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('m-mobile-hide');
        });

        $('.ui.dropdown').dropdown({
            on : 'hover'
        });
        function page(obj) {
            $("[name='page']").val($(obj).data("page"));
            loaddata();
        }
        //消息提示关闭初始化
        $('.message .close')
            .on('click', function () {
                $(this)
                    .closest('.message')
                    .transition('fade');
            });
        $('#clear-btn')
            .on('click', function() {
                $('.ui.type.dropdown')
                    .dropdown('clear')
                ;
            });
        $("#search-btn").click(function () {
            $("[name='page']").val(0);
            loaddata();
        });
        function loaddata() {
            $("#table-container").load(/*[[@{/admin/blogs/search}]]*/"/admin/blogs/search",{
                title : $("[name='title']").val(),
                typeId : $("[name='typeId']").val(),
                recommend : $("[name='recommend']").prop('checked'),
                page : $("[name='page']").val()
            });
        }
    </script>
</body>
</html>

6、渲染表单
在这里插入图片描述
在这里插入图片描述
7,链接数据库
在这里插入图片描述
链接成功
在这里插入图片描述
9、修改typeService

package net.zl.myblog.service;


import net.zl.myblog.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;


/**
 * 分类服务
 */
public interface TypeService {

    Type saveType(Type type);//新增

    Type getType(Long id);//根据id查询

    Type getTypeByName(String name);//根据名称来查询

    Page<Type> listType(Pageable pageable);//分布查询

    List<Type> listType();

    Type updateType(Long id, Type type);//根据id修改

    void deleteType(Long id);//删除
}

10、实现接口方法
在这里插入图片描述
11、运行,查看结果
出现空指针错误
新建一个查询类BlogQuery

package net.zl.myblog.vo;

/**
 *查询类
 */
public class BlogQuery {

    private String title;
    private Long typeId;
    private boolean recommend;

    public BlogQuery() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }
}

修改BlogService接口

package net.zl.myblog.service;


import net.zl.myblog.po.Blog;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;


/**
 *
 */
public interface BlogService {
    Blog getBlog(Long id);//根据id查询
    Page<Blog> listBlog(Pageable pageable, BlogQuery blog);//分页查询
    Blog saveBlog(Blog blog);//新增
    Blog updateBlog(Long id,Blog blog);//更新
    void deleteBlog(Long id);//删除
}

修改BlogController.java

package net.zl.myblog.web.admin;


import net.zl.myblog.po.Blog;
import net.zl.myblog.po.User;
import net.zl.myblog.service.BlogService;
import net.zl.myblog.service.TagService;
import net.zl.myblog.service.TypeService;
import net.zl.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


/**
 * blogs控制器
 */
@Controller
@RequestMapping("/admin")
public class BlogController {
    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model) {
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs :: blogList";
    }
   
}

修改BlogServiceImpl.java
在这里插入图片描述
效果
在这里插入图片描述
二、博客新增
1、修改新增页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/lib/editormd/css/editormd.min.css">
    <link rel="stylesheet" href="../../static/css/me.css">
    <title>博客发布</title>
</head>
<body>
    <!--导航-->
    <nav class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
        <div class="ui container">
            <div class="ui inverted secondary stackable menu">
                <h2 class="ui teal header item">管理后台</h2>
                <a href="#" class="active m-item item m-mobile-hide"><i class="mini home icon"></i>博客</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini idea icon"></i>分类</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini tags icon"></i>标签</a>
                <div class="right m-item m-mobile-hide menu">
                    <div class="ui dropdown item">
                        <div class="text">
                            <img src="https://unsplash.it/100/100?image=1005" alt="" class="ui avatar image">
                            周璐
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="#" class="item">注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show"  >
            <i class="sidebar icon"></i>
        </a>
    </nav>
    <!--二级导航-->
    <div class="ui attached pointing menu">
        <div class="ui container">
            <div class="right menu">
                <a href="#" th:href="@{/admin/blogs/input}" class="teal active item">发布</a>
                <a href="#" th:href="@{/admin/blogs}" class="item">列表</a>
            </div>
        </div>
    </div>
    <!--中间内容-->
    <div class="m-container m-padded-tb-big">
        <div class="ui container">
            <form id="blog-form" action="#" th:object="${blog}" th:action="@{/admin/blogs}" method="post" class="ui form">                <input type="hidden" name="published" th:value="*{published}">
                <input type="hidden" name="published" th:value="*{published}">
                <input type="hidden" name="id" th:value="*{id}">
                <!--标题加原创等-->
                <div class="required field">
                    <div class="ui left labeled input">
                        <div class="ui selection compact teal basic dropdown label">
                            <input type="hidden" value="原创" name="flag" th:value="*{flag}">
                            <i class="dropdown icon"></i>
                            <div class="text">原创</div>
                            <div class="menu">
                                <div class="item" data-value="原创">原创</div>
                                <div class="item" data-value="转载">转载</div>
                                <div class="item" data-value="翻译">翻译</div>
                            </div>
                        </div>
                        <input type="text" name="title" placeholder="标题" th:value="*{title}">
                    </div>
                </div>
                <!--内容-->
                <div class="required field">
                    <div id="md-content" style="z-index: 1 !important;">
                        <textarea placeholder="博客内容" name="content" style="display: none" th:text="*{content}"></textarea>
                    </div>
                </div>
                <!--分类与标签-->
                <div class="two fields">
                    <!--分类-->
                    <div class="required field">
                        <div class="ui left labeled action input">
                            <label class="ui compact teal basic label">分类</label>
                            <div class="ui fluid selection dropdown">
                                <input type="hidden" name="type.id" th:value="*{type}!=null ? *{type.id}">
                                <i class="dropdown icon"></i>
                                <div class="default text">分类</div>
                                <div class="menu">
                                    <div th:each="type : ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">错误日志</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--标签-->
                    <div class="field">
                        <div class="ui left labeled action input">
                            <label class="ui compact teal basic label">标签</label>
                            <div class="ui tag fluid selection multiple search  dropdown"> <!--multiple:多选-->
                                <input type="hidden" name="tagIds" th:value="*{tagIds}" >
                                <i class="dropdown icon"></i>
                                <div class="default text">标签</div>
                                <div class="menu">
                                    <div th:each="tag : ${tags}" class="item" data-value="1" th:data-value="${tag.id}" th:text="${tag.name}">java</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!--首图-->
                <div class="required field">
                    <div class="ui left labeled input">
                        <label  class="ui teal basic label">首图</label>
                        <input type="text" name="firstPicture" th:value="*{firstPicture}" placeholder="首图引用地址">
                    </div>
                </div>
                <!--博客描述-->
                <div class="required field">
                    <textarea name="description" th:text="*{description}" placeholder="博客描述..." maxlength="200"></textarea>
                </div>
                <!--选择框(推荐等)-->
                <div class="inline fields">
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="recommend" name="recommend" checked th:checked="*{recommend}" class="hidden">
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="shareStatement" name="shareStatement" th:checked="*{shareStatement}" class="hidden">
                            <label for="shareStatement">转载声明</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="appreciation" name="appreciation" th:checked="*{appreciation}" class="hidden">
                            <label for="appreciation">赞赏</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="commentabled" name="commentabled" th:checked="*{commentabled}" class="hidden">
                            <label for="commentabled">评论</label>
                        </div>
                    </div>
                </div>
                <!--放错误信息的-->
                <div class="ui error message"></div>
                <!--按钮-->
                <div class="ui right aligned container">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <button type="button" id="save-btn" class="ui secondary button">保存</button>
                    <button type="button" id="publish-btn" class="ui teal button">发布</button>
                </div>
            </form>
        </div>
    </div>
    <!--底部footer-->
    <footer  th:replace="admin/_fragments :: footer" class="ui inverted  vertical segment m-padded-tb-massive">
        <div class="ui center aligned container">
            <div class="ui inverted divided stackable grid">
                <div class="three wide column">
                    <div class="ui inverted link list">
                        <div class="item">
                            <img src="../../static/images/wechat.jpg" class="ui rounded image" style="width: 110px">
                        </div>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">最新博客</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">联系我</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">Email:2392347073@qq.com</a>
                        <a href="#" class="item">QQ:2392347073</a>
                    </div>
                </div>
                <div class="seven wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">Lirenmi</h4>
                    <p class="">这是我的个人博客,会分享关于编程、写作、思考相关的任何内容,希望可以给来到这里的人有所帮助...</p>
                </div>
            </div>
            <div class="ui inverted section divider"></div>
            <p class="m-text-thin m-text-spaced m-opacity-tiny">Copyright  2016-2017 Lirenmi DESIGNED BYlIRENMI </p>
        </div>

    </footer>

    <!--/*/<th:block th:replace="admin/_fragments :: script">/*/-->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script>
    <script src="../../static/lib/editormd/editormd.min.js"></script>
    <!--/*/</th:block>/*/-->
    <script>
        //初始化markdown编辑器
        var ocontentEditor;
        $(function() {
            ocontentEditor = editormd("md-content", {
                width   : "100%",
                height  : 640,
                syncScrolling : "single",
                // path    : "../static/lib/editormd/lib/"
                path    : "/lib/editormd/lib/"
            });
        });

        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('m-mobile-hide');
        });

        $('.ui.dropdown').dropdown({
            on : 'hover'
        });

        $('#save-btn').click(function () {
            $('[name="published"]').val(false);
            $('#blog-form').submit();
        });

        $('#publish-btn').click(function () {
            $('[name="published"]').val(true);
            $('#blog-form').submit();
        });
        $('.ui.tag.dropdown')
            .dropdown({
                allowAdditions: true
            });
        $('.ui.form').form({
            fields : {
                title : {
                    identifier: 'title',
                    rules: [{
                        type : 'empty',
                        prompt: '标题:请输入博客标题'
                    }]
                },
                content : {
                    identifier: 'content',
                    rules: [{
                        type : 'empty',
                        prompt: '标题:请输入博客内容'
                    }]
                },
                typeId : {
                    identifier: 'type.id',
                    rules: [{
                        type : 'empty',
                        prompt: '标题:请输入博客分类'
                    }]
                },
                firstPicture : {
                    identifier: 'firstPicture',
                    rules: [{
                        type : 'empty',
                        prompt: '标题:请输入博客首图'
                    }]
                },
                description : {
                    identifier: 'description',
                    rules: [{
                        type : 'empty',
                        prompt: '标题:请输入博客描述'
                    }]
                }
            }
        });

    </script>
</body>
</html>

2,页面效果图
在这里插入图片描述
3,添加相应的方法

package net.zl.myblog.service;

import net.zl.myblog.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;


/**
 *
 */
public interface TagService {

    Tag saveTag(Tag tag);//新增
    Tag getTag(Long id);//查询
    Tag getTagByName(String name);//根据名称查询
    Page<Tag> listTag(Pageable pageable);//分页查询
    List<Tag> listTag();
    Tag updateTag(Long id,Tag tag);//更新
    void deleteTag(Long id);//删除
}

在这里插入图片描述
4,修改blog-input.html代码
在这里插入图片描述
5,修改编辑器的路径
在这里插入图片描述
最终效果图
在这里插入图片描述
6,添加接口属性

package net.zl.myblog.service;


import javassist.NotFoundException;
import net.zl.myblog.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;


/**
 *
 */
public interface TagService {

    Tag saveTag(Tag tag);//新增
    Tag getTag(Long id);//查询
    Tag getTagByName(String name);//根据名称查询
    Page<Tag> listTag(Pageable pageable);//分页查询
    List<Tag> listTag();
    List<Tag> listTag(String ids);
    Tag updateTag(Long id,Tag tag);//更新
    void deleteTag(Long id);//删除
}

7,添加实现方法
在这里插入图片描述
8,定义一个属性对象,并重写
在这里插入图片描述
9,修改blogs.html代码
在这里插入图片描述
在这里插入图片描述
测试发布
成功发布
在这里插入图片描述
10,搜索框清零
在这里插入图片描述
在blogs.html中添加清除按钮

在这里插入图片描述
效果
在这里插入图片描述
三、博客编辑
1,修改blogs.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/me.css">
    <title>博客管理</title>
</head>
<body>
    <!--导航-->
    <nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment m-padded-tb-mini m-shadow-small">
        <div class="ui container">
            <div class="ui inverted secondary stackable menu">
                <h2 class="ui teal header item">管理后台</h2>
                <a href="#" class="active m-item item m-mobile-hide"><i class="mini home icon"></i>博客</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini idea icon"></i>分类</a>
                <a href="#" class="m-item item m-mobile-hide"><i class="mini tags icon"></i>标签</a>
                <div class="right m-item m-mobile-hide menu">
                    <div class="ui dropdown item">
                        <div class="text">
                            <img src="https://unsplash.it/100/100?image=1005" alt="" class="ui avatar image">
                            周璐
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="#" class="item">注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show"  >
            <i class="sidebar icon"></i>
        </a>
    </nav>
    <!--二级导航-->
    <div class="ui attached pointing menu">
        <div class="ui container">
            <div class="right menu">
                <a href="#" th:href="@{/admin/blogs/input}" class="item">发布</a>
                <a href="#" th:href="@{/admin/blogs}" class="teal active item">列表</a>
            </div>
        </div>
    </div>
    <!--中间内容-->
    <div class="m-container-small m-padded-tb-big">
        <div class="ui container">
            <!--中间头部表单-->
            <div  class="ui secondary segment form">
                <input type="hidden" name="page" >
                <div class="inline fields">
                    <!--标题-->
                    <div class="field">
                        <input type="text" name="title" placeholder="标题">
                    </div>
                    <!--分类-->
                    <div class="field">
                        <div class="ui labeled action input">
                            <div class="ui type selection dropdown">
                                <input type="hidden" name="type">
                                <i class="dropdown icon"></i>
                                <div class="default text">分类</div>
                                <div class="menu">
                                    <div th:each="type : ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">错误日志</div>
                                    <!--/*-->
                                    <div class="item" data-value="2">开发者手册</div>
                                    <!--*/-->
                                </div>
                            </div>
                            <button id="clear-btn" class="ui compact button">clear</button>
                        </div>
                    </div>
                    <!--推荐-->
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="recommend" name="recommend">
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <!--搜索-->
                    <div class="field">
                        <button type="button" id="search-btn" class="ui mini teal basic button"><i class="search icon"></i>搜索</button>
                    </div>
                </div>
            </div>
            <div id="table-container">
                <!--中间表格-->
                <table th:fragment="blogList" class="ui compact teal table">
                    <!--表头-->
                    <thead>
                    <tr>
                        <th></th>
                        <th>标题</th>
                        <th>类型</th>
                        <th>推荐</th>
                        <th>状态</th>
                        <th>更新时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <!--表格内容-->
                    <tbody>
                    <tr th:each="blog,iterStat : ${page.content}">
                        <td th:text="${iterStat.count}">1</td>
                        <td th:text="${blog.title}">刻意练习清单</td>
                        <td th:text="${blog.type.name}">认知升级</td>
                        <td th:text="${blog.recommend} ? '':''"></td>
                        <td th:text="${blog.published} ? '发布':'草稿'">草稿</td>
                        <td th:text="${blog.updateTime}">2017-10-02 09:45</td>
                        <td>
                            <a href="#" th:href="@{/admin/blogs/{id}/input(id=${blog.id})}" class="ui mini teal basic button">编辑</a>
                            <a href="#" th:href="@{/admin/blogs/{id}/delete(id=${blog.id})}" class="ui mini red basic button">删除</a>
                        </td>
                    </tr>
                    </tbody>
                    <!--表格底部-->
                    <tfoot>
                    <tr>
                        <th colspan="7">
                            <div class="ui mini pagination menu" th:if="${page.totalPages}>1" >
                                <a onclick="page(this)" th:attr="data-page=${page.number}-1" class="item" th:unless="${page.first}">上一页</a>
                                <a onclick="page(this)" th:attr="data-page=${page.number}+1" class=" item" th:unless="${page.last}">下一页</a>
                            </div>
                            <a href="#" th:href="@{/admin/blogs/input}" class="ui mini right floated teal basic button">新增</a>
                        </th>
                    </tr>
                    </tfoot>
                </table>

                <div class="ui success message" th:unless="${#strings.isEmpty(message)}">
                    <i class="close icon"></i>
                    <div class="header">提示:</div>
                    <p th:text="${message}">恭喜,操作成功!</p>
                </div>
            </div>
        </div>
    </div>
    <!--底部footer-->
    <footer th:replace="admin/_fragments :: footer" class="ui inverted  vertical segment m-padded-tb-massive">
        <div class="ui center aligned container">
            <div class="ui inverted divided stackable grid">
                <div class="three wide column">
                    <div class="ui inverted link list">
                        <div class="item">
                            <img src="../../static/images/wechat.jpg" class="ui rounded image" style="width: 110px">
                        </div>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">最新博客</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                        <a href="#" class="item">用户故事(User Story)</a>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">联系我</h4>
                    <div class="ui inverted link list">
                        <a href="#" class="item">Email:2392347073@qq.com</a>
                        <a href="#" class="item">QQ:2392347073</a>
                    </div>
                </div>
                <div class="seven wide column">
                    <h4 class="ui inverted header m-text-thin m-text-spaced ">Lirenmi</h4>
                    <p class="">这是我的个人博客,会分享关于编程、写作、思考相关的任何内容,希望可以给来到这里的人有所帮助...</p>
                </div>
            </div>
            <div class="ui inverted section divider"></div>
            <p class="m-text-thin m-text-spaced m-opacity-tiny">Copyright  2016-2017 Lirenmi DESIGNED BYlIRENMI </p>
        </div>

    </footer>
    <!--/*/<th:block th:replace="admin/_fragments :: script">/*/-->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script>
    <!--/*/</th:block>/*/-->
    <script>
        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('m-mobile-hide');
        });

        $('.ui.dropdown').dropdown({
            on : 'hover'
        });
        function page(obj) {
            $("[name='page']").val($(obj).data("page"));
            loaddata();
        }
        //消息提示关闭初始化
        $('.message .close')
            .on('click', function () {
                $(this)
                    .closest('.message')
                    .transition('fade');
            });
        $('#clear-btn')
            .on('click', function() {
                $('.ui.type.dropdown')
                    .dropdown('clear')
                ;
            });
        $("#search-btn").click(function () {
            $("[name='page']").val(0);
            loaddata();
        });
        function loaddata() {
            $("#table-container").load(/*[[@{/admin/blogs/search}]]*/"/admin/blogs/search",{
                title : $("[name='title']").val(),
                typeId : $("[name='typeId']").val(),
                recommend : $("[name='recommend']").prop('checked'),
                page : $("[name='page']").val()
            });
        }
    </script>
</body>
</html>

2、初始化blogIds
在这里插入图片描述
3、修改blogcontroller代码
在这里插入图片描述
在这里插入图片描述
4、运行查看效果
能进行修改

四、博客删除
1、添加删除方法
在这里插入图片描述
2、运行效果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值