Martin
Martin

Reputation: 127

Spring Boot Thymeleaf th:include dynamic templates

I am using Spring Boot v1.5.2.RELEASE and Thymeleaf.

I use <div th:include="praxis/header"></div>. It works fine, but now I have a special requirement that I want to include a path in the controller like this:

<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);
    }
}

It does not work because th:include can only include templates from the "resource" folder.

How can I include a template from the controller?

Upvotes: 1

Views: 1086

Answers (1)

SHG
SHG

Reputation: 2616

th:include can only include fragments from other templates, it cannot include data from the controller.

If you want to send data from the controller to the template you should create a map, set the content you want to send as the value of some key in this map, and send this map as model using this API:

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

Now, you have it in the template and you can use it in any way you want.

Upvotes: 1

Related Questions