java - 在 thymeleaf 中使用 th:if 和 th:replcae 动态更改页面
问题描述
这是我的控制器:
@RequestMapping(value="/user/edit/{id}", method = RequestMethod.GET)
public String editForm(@PathVariable Integer id,Model model) {
model.addAttribute("UserInfo", userService.getUserById(id));
return "EditUser";
}
@RequestMapping(value="/moduleList", method = RequestMethod.GET)
public String moduleList(Model model) {
model.addAttribute("moduleCommand", new Module());
model.addAttribute("moduleList", applicationService.getAllModules());
return "modulelist";
}
rightSection.html
<div th:fragment="rightSectionBar">
<div class="container body">
<div class="title_section" style="margin-top: 50px;">
<div class="col-md-2 heading">
<h4>Home</h4>
</div>
</div>
<div class="col-md-9 right_col">
<div class="clearfix"></div>
<!-- page content -->
<span th:replace="fragments/contents::dashboard"></span>
//I want to see below content on calling "/user/edit/{id}" url
<span th:replace="fragments/EditUser::editUsersBar"></span>
//I want to see below content on calling "/moduleList" url
<span th:replace="fragments/modulelist::modulesListBar"></span>
</div>
</div>
</div>
我试图在每个页面上保持侧面菜单和顶部导航相同,并且只想更改右侧部分内容。
解决方案
你可以使用th:block
andth:if
来达到这个目的。Thymeleaf 将执行其中的属性th:block
,使其消失,因为它th:block
只是一个属性容器。
<div class="col-md-9 right_col">
<div class="clearfix"></div>
<span th:replace="fragments/contents::dashboard"></span>
<th:block th:if="${UserInfo}">
<span th:replace="fragments/EditUser::editUsersBar"></span>
</th:block>
<th:block th:if="${moduleCommand}">
<span th:replace="fragments/modulelist::modulesListBar"></span>
</th:block>
</div>
推荐阅读
- node.js - NPM /.config 和 /.npm 不存在。OS X 不允许我创建。NPM 不会下载任何包
- angular - 使用角度 8 设置自定义类
- c# - C#:具有相同命名空间的引用程序集
- scala - scala:附加两个列表
- azure - Azure Functions 使用应用服务计划:根据验证程序,远程证书无效
- python - TypeError 'bool' 对象不可下标
- perl - Mojolicious - 如何在控制器方法处理和呈现之前拦截传入的请求
- c# - 使用 c# 中的性能计数器在给定进程的结果中获取 0 字节
- reactjs - 我的 If Else 条件在 React.js 中无法正常工作
- javers - Javers 生产最佳实践