基于 Spring Boot 博客系统开发(七)

基于 Spring Boot 博客系统开发(七)

本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿
基于 Spring Boot 博客系统开发(六)👈👈

后台公共代码抽取

添加访问后台模板页的方法,AdminController

java 复制代码
@Controller
@RequestMapping("/admin")
public class AdminController {

    @RequestMapping("/")
    public String home(){
        return "admin/index";
    }
    
    @RequestMapping("/list")
    public String list(){
        return "admin/list";
    }
    
    @RequestMapping("/edit")
    public String edit(){
        return "admin/edit";
    }

}

创建 include.html 用于存储公共部分代码,使用 th:fragment

抽取公共代码到 include 文件中

引用公共代码

index.html 整理后代码

list.html 整理后代码

edit.html 整理后代码,非公共代码需要保留

实现点击菜单高亮

基于上面公共代码抽取后,但是不同页面所需要公共代码需要进行调整,就需要主页面传递参数到公共代码模板中。

admin/index.html 页面中,传递了实参1值。

html 复制代码
<th:block th:include="admin/include :: header-menu(1)" />

抽取页面 admin/include 中,可以定义形参tag。使用tag形参配合 th:classappend 添加样式,这里顺便修改了a标签链接。

html 复制代码
<div th:fragment="header-menu(tag)" >
......这里省略
 					<ul>
                        <li>
                            <a href="/admin/" class="waves-effect " th:classappend="${tag == 1?'active':''}">
                                <i class="fa fa-dashboard" aria-hidden="true"></i><span> 仪表盘 </span>
                            </a>
                        </li>
                        <li >
                            <a href="/admin/edit" class="waves-effect" th:classappend="${tag == 2?'active':''}">
                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i><span> 发布文章 </span></a>
                        </li>
                        <li>
                            <a href="/admin/list" th:classappend="${tag == 3?'active':''}"
                               class="waves-effect"><i
                                    class="fa fa-list" aria-hidden="true"></i><span> 文章管理 </span></a>
                        </li>
                    </ul>
......这里省略
</div>

实现效果,点击菜单高亮显示

相关推荐
不爱编程的小九九2 小时前
小九源码-springboot088-宾馆客房管理系统
java·开发语言·spring boot
抹香鲸之海3 小时前
Prometheus+Grafana实现Springboot服务监控
spring boot·grafana·prometheus
摇滚侠3 小时前
Spring Boot 3零基础教程,WEB 开发 内嵌服务器底层源码分析 笔记48
spring boot·笔记
萌新小码农‍4 小时前
SpringBoot+alibaba的easyexcel实现前端使用excel表格批量插入
前端·spring boot·excel
韩立学长5 小时前
【开题答辩实录分享】以《租房小程序的设计和实现》为例进行答辩实录分享
java·spring boot·小程序
zl9798995 小时前
SpringBoot-数据访问之MyBatis与Redis
java·spring boot·spring
重生之我是Java开发战士5 小时前
【Java EE】快速上手Spring Boot
java·spring boot·java-ee
不爱编程的小九九8 小时前
小九源码-springboot099-基于Springboot的本科实践教学管理系统
java·spring boot·后端
lang201509288 小时前
Spring Boot集成Spring Integration全解析
spring boot·后端·spring
武子康8 小时前
Java-155 MongoDB Spring Boot 连接实战 | Template vs Repository(含索引与常见坑)
java·数据库·spring boot·后端·mongodb·系统架构·nosql