基于 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>

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

相关推荐
计算机学姐34 分钟前
基于微信小程序的智能在线预约挂号系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·tomcat
笨蛋不要掉眼泪3 小时前
SpringBoot项目Excel模板下载功能详解
java·spring boot·后端·spring·excel·ruoyi
麦兜*7 小时前
Redis监控告警体系搭建:使用Redis Exporter + Prometheus + Grafana
java·spring boot·redis·spring·spring cloud·grafana·prometheus
风象南9 小时前
告别日志“大海捞针”,基于SpringBoot的错误指纹聚类实现
spring boot·后端
Q_Q196328847511 小时前
python+springboot+uniapp基于微信小程序的校园二手闲置二手交易公益系统 二手交易+公益捐赠
spring boot·python·django·flask·uni-app·node.js·php
悟能不能悟11 小时前
springboot用jar启动能访问,但是打成war,部署到tomcat却访问不到
spring boot·tomcat·jar
Q_Q196328847513 小时前
python+spring boot洪涝灾害应急信息管理系统 灾情上报 预警发布 应急资源调度 灾情图表展示系统
开发语言·spring boot·python·django·flask·node.js·php
李昊哲小课18 小时前
Spring Boot 基础教程
java·大数据·spring boot·后端