侧边栏希望能做到点击之后改变颜色或者样式,让用户知道自己所在的目录
这里只是简单的例子,具体可以查看usingthymeleaf的pdf文档
第一步:给引入的侧边栏传入固定的参数,当打开这个页面的时候固定的向侧边栏传入参数
<div th:replace="~{commons/bar :: #sidebar(activeUri='emps')}"></div>
这里向id为sidebar的片段传入activeUri。可以通过逗号分隔来添加多个变量名
第二步:在侧边栏的片段部分的class标签做判断
<li class="nav-item">
<a
th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}"
th:href="@{/main.html}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
<polyline points="9 22 9 12 15 12 15 22"></polyline>
</svg>
Dashboard <span class="sr-only">(current)</span>
</a>
</li>
这里用来(?a:b)三元表达式做判断,通过参数选择样式