Description
Hi,
Spring boot version: 2.6.6
Java: 17
I am trying to write a controller action that returns multiple html elements based on a selector. This works when the items matching the selector are in the same template, but any matching elements that are in fragments included in the template are ignored.
Example:
Controller
@Controller
public class IndexController {
@GetMapping("")
public String showIndex() {
return "index";
}
@GetMapping("reloadItems")
public String reloadItems() {
return "index :: %reload"; // <- only return elements with th:ref="reload" attribute
}
}
index.html
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:replace="header :: header"></div>
<p th:ref="reload">An Item to reload</p>
<p th:ref="reload">Another Item to reload</p>
<button>Reload</button>
<script src="webjars/jquery/3.6.0/jquery.min.js"></script>
<script>
$("button").click(function () {
$.get("reloadItems", function(data){
console.log(data);
}, "html");
});
</script>
</body>
</html>
header.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:fragment="header">
<p th:ref="reload">Header to reload</p>
</div>
</body>
</html>
When the "Reload" button is clicked only <p>An Item to reload</p><p>Another Item to reload</p>
are returned. The element in the header.html fragment is not.
If I move the element from header.html into index.html all elements are returned by the controller.
Is this expected behaviour? I would have expected the returned data to include the element from the included fragment.
I have attached a copy of my example Spring boot app.