javascript - Hotw 将片段的动态数据加载到 thymeleaf 项目中的另一个 HTML 页面
问题描述
我有一个带有切换和折叠组件的项目的动态加载版本(动态 URL)的侧边栏导航,当我单击展开按钮时,它将显示 URL 列表。当我从侧边栏中单击任何 URL 时,索引页面将再次根据从侧边栏中选择的版本显示带有动态 URL 的数据。当我单击索引页面上的 URL 时,它会重定向到另一个页面,但侧边栏中没有数据。
I want the dynamic data loaded in fragment to be passed to all the pages using thymeleaf?
Here is an example:
nav.HTML
<div th:fragment="sidebar2">
<div class="container">
<nav id="sidebarMenu"
class="col-md-3 col-lg-2 d-md-block sidebar collapse bg-light">
<div class="sidebar-sticky pt-3">
<ul class="nav flex-column" id="accordionSidebar">
<li class="nav-item"><a type="button" id="collapse"
data-toggle="collapse" data-target="#collapseExample"
aria-expanded="false" aria-controls="collapseExample"> <span
class="menu-title">PI Versions</span>
</a>
<div class="collapse" id="collapseExample">
<ul class="nav flex-column sub-menu" id="collapseExample2">
<li th:each="color : ${coloring}"><a
th:href="@{/toList(color=${color})}" th:text="${color}"> ></a></li>
</ul>
</div></li>
</ul>
</div>
</nav>
</div>
</div>
Here is my index.html:
<div th:replace="fragments/nav :: sidebar2"></div>
<main role="main" class="container">
<div class="starter-template">
<h1>Spring Boot Web Thymeleaf Example</h1>
<h2>
<span th:text="'Hello, ' + ${message}"></span>
</h2>
</div>
<ol>
<li th:each="color2 : ${colors}" th:text="${color2}"></li>
</ol>
<span th:with="convertedList=${#lists.toList(colors)}">
converted list size: <span th:text="${#lists.size(convertedList)}"/></span>
</span>
这是我的java控制器代码:
@Controller 公共类 HomeController {
@RequestMapping("/")
public ModelAndView usingToList(Model model) {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("yellow");
colors.add("red");
colors.add("blue");
model.addAttribute("colors", colors);
List<String> colors2 = new ArrayList<>();
colors2.add("pinkish");
colors2.add("green");
colors2.add("yellow");
colors2.add("red");
colors2.add("blue");
model.addAttribute("coloring", colors2);
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
@GetMapping("/toList")
public String usingToList2(Model model,String color) {
List<String> colors2 = new ArrayList<>();
if(color.equals("pinkish"))
{
colors2.add("pinkish");
colors2.add("amity");
colors2.add("pimity");
}
model.addAttribute("colors", colors2);
return "index";
}
}
任何人都可以帮助我实现这一目标,在此先感谢。
解决方案
不确定是否很好地理解了这个问题,但是在使用此链接时:
th:href="@{/toList(color=${color})}"
您将参数传递给您需要在控制器中检索的 URL,例如:
public String usingToList2(Model model,@RequestParam(name = "color" String color) {...}
也许会有所帮助。
推荐阅读
- android - 无法在 android studio 3.3.1 中解析符号“R”
- android-studio - 如何在 Windows 中使用 android studio 配对 mac 以测试颤振应用程序
- python - 将输入输入到 keras 序列模型中
- sql - 与 PostgreSQL 具有一对多关系的 SQL 查询
- c++ - 在 Crypto++ 中使用 Curve25519 签名
- python - 使用映射将 DataFrame 从数字转换为字符串
- c - 关于手册页中无效使用的 setbuf 示例的混淆
- java - 在 2 个 REST 资源之间创建关系
- react-apollo - 加载获取分页请求时显示的文本
- c - 为什么这不会产生错误?