1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry response late,i resolved this problem with your help,thanks a lot :) . I write a controller code(@Controller @RequestMapping(path = "/praxis")) and return a model code(ModelAndView modelAndView = new ModelAndView("praxis/space", "user", user);) and in this page use code(<div th:include="praxis/header"></div>) ,it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.