How to pass a parameter from a Java controller to a Thymeleaf template

Viewed 501

I'm just trying to pass a parameter from a controller to a Thymeleaf template. From the Thymeleaf documentation I understand that I can do it as below:

return "editor/{id}(id=" + newEditor.getIdEditor() + ")";

There is a function into the controller which receives the request

@GetMapping(value = "/editor/{id}")

but all I get is an error

Error resolving template [editor/{id}(id=8)],

Maybe my approach is wrong and there is a proper way to do it, but I haven't found it.

1 Answers

The return-value from controller method should be a Thymeleaf template name, and that is what the error message tries to tell you.

If you are trying to return a value for use in your Thymeleaf template:

@GetMapping("/yourendpointname")
public String yourControllerMethod(final Model model) {

    ...

    model.addAttribute("idEditor", newEditor.getIdEditor());
    return "template_name";
}

You can now assemble the URL from your question inside the Thymeleaf template.

Related