我有一个HTML5页面(大致!)以下结构:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<form>
<div id="a">
<div id="i">
// content i
</div>
<div id="x">
// some content (several lines of code)
</div>
</div>
<div id="b">
<div id="j">
// content j
</div>
<div id="x">
// some content (several lines of code)
</div>
</div>
<div id="c">
<div id="k">
// content k
</div>
<div id="x">
// some content (several lines of code)
</div>
</div>
</div>
// <div id="x" /> definition location
</form>
</body>
</html>
为了实现一些易于维护,是否可以在页面的某处定义<div id="x" />
并在需要时调用它?类似于(但不完全!)<div th:replace="fragments/x :: xFragment />
?
为了将其值传递给控制器,我认为应该在表单中定义<div id="x" />
,比如说,我在上面的代码// <div id="x" /> definition location
上的位置。
答案 0 :(得分:2)
这未经过测试,但可能是一种开始的方法:
<div th:fragment="insideFragment(id, id2, content)">
<div th:id="${id2}">
<div th:text="${content}" th:remove="tag" />
</div>
<div id="x">// some content</div>
</div>
<div th:fragment="myFragment(ids, subids, contents)">
<th:block th:each="content, i : ${contents}">
<div th:replace="PATH :: insideFragment(${ids[i.index]}, ${subids[i.index]}, ${content})" />
</th:block>
</div>