Spring Boot Thymeleaf th:包含动态模板

时间:2017-03-30 02:28:32

标签: spring spring-mvc thymeleaf

我正在使用Spring Boot v1.5.2.RELEASE和Thymeleaf。

我使用<div th:include="praxis/header"></div>。 它工作正常,但现在我有一个特殊的要求,我想在控制器中包含一个路径,如下所示:

<div th:include="praxis/header"></div>

@Controller
@RequestMapping(path = "/praxis")
public class UserController extends BaseController {
@GetMapping(value = "/header")
    public ModelAndView praxisHeader(HttpServletRequest request) {
        //do sth
        return new ModelAndView("some other templates", "user", user);
    }
}

它不起作用,因为th:include只能包含来自&#34;资源&#34;的模板。文件夹中。

如何从控制器中包含模板?

1 个答案:

答案 0 :(得分:1)

th:include只能包含来自其他模板的片段,它不能包含来自控制器的数据。

如果要将数据从控制器发送到模板,则应创建地图,将要发送的内容设置为此地图中某个键的值,然后使用此地址将此地图发送为model API:

public ModelAndView(String viewName, Map<String,?> model)

现在,您可以在模板中使用它,并且可以以任何方式使用它。