Reputation: 1381
this is my Spring Boot with Thymeleaf setup.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
ThymeleafConfig
@Configuration
public class ThymeleafConfig {
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("LEGACYHTML5");
resolver.setOrder(1);
return resolver;
}
}
Controller
@RestController
public class WebController {
@RequestMapping("")
public String index(){
return "index";
}
}
Index.html is located in src/main/resources/templates.
But when localhost:8080 is called only "index" string is rendered. Index.html is not fetched. What could be the problem?
Upvotes: 2
Views: 1323
Reputation: 279960
You're using a @RestController
.
All handler methods of a @RestController
bean act as if annotated with @ResponseBody
, ie. the object they return is written to the response directly based on some HttpMessageConverter
.
Change @RestController
to @Controller
if you don't want that behavior.
Upvotes: 6