Для простоты, давайте предположим, что у нас есть файл шаблона html ( test.htm ), подобный этому:
<div th:fragment="test">
this is a test fragment
</div>
<div th:include=":: test"> <!-- we'll change this line later -->
this is a placeholder
</div>
И следующий контроллер используется для возврата test.htm:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView get(ModelAndView mav) {
mav.setViewName("/test.htm");
mav.addObject("fragmentName", ":: test"); // we'll use this later
return mav;
}
}
В этом случае мы можем получить следующий результат, если мы получим доступ к домашнему индексу:
this is a test fragment
this is a test fragment
Но если мы используем переменную fragmentName
для установки th: include valueкак это:
<div th:fragment="test">
this is a test fragment
</div>
<div th:include="${fragmentName}"> <!-- modified to use a variable value -->
this is a placeholder
</div>
Thymeleaf жалуется, что этот шаблон ":: test" не может быть разрешен:
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [:: test], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "/test.htm" - line 5, col 6)
Здесь возникает вопрос: есть ли способ установить th:include
значение с помощью переменной?