I have an app which is a simplified version of the Spring Bookmark tutorial. In it, Controllers are annotated with @RestController
and the app only returns JSON.
I've added to mine the ability to return HTML via Thymeleaf templates. My templates are being returned, but they don't seem to be processed by Thymeleaf. I'm using spring-boot, and I've spring-boot-starter-thymeleaf
to my build.gradle
file, but that doesn't seem to be enough.
For example, here's a simple controller for the root:
package com.latencyzero.hoa;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
class
MainController
{
@RequestMapping(method = RequestMethod.GET)
ModelAndView
index()
{
ModelAndView mav = new ModelAndView("index");
mav.addObject("version", "0.1");
return mav;
}
}
and src/main/resources/templates/index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>HOA v${version}</title>
</head>
<body>
<h1>HOA v${version}</h1>
</body>
</html>
Results in the following page being rendered:
The examples I've found suggest this is all I need to do, but it's not working. Do I need some additional configuration annotations somewhere?
Thanks.
@RestController
will not work you need a@Controller
for that. – M. Deinum@RestController
is just a@Controller
with@ResponseBody
. Nevertheless, it doesn't work with@Controller
, either. – Rickth:text
you cannot (afaik) use inline variables with thyme leaf. – M. Deinum