0

My problem is that I fail to show a Thymeleaf template. I suspect a config error, but I can't seem to find it. Thanks in advance :)

pom.xml:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> ... <properties> ... <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.3</thymeleaf-layout-dialect.version> </properties> ... <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> <dependency> <groupId>nz.net.ultraq.thymeleaf</groupId> <artifactId>thymeleaf-layout-dialect</artifactId> </dependency> 

Template is in: src/main/resources/templates/home.html

Spring config:

server.servlet.context-path=/mityo server.port=8080 ... spring.thymeleaf.enabled=true spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.mode=html5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.suffix=.html spring.thymeleaf.prefix=/resources/templates/ 

Controller:

@Controller @RequestMapping("buy") public class HomeController { public String buy(Model model) { return "home"; } } 

I'm trying to access: localhost:8080/mityo/buy and getting 404.

Another thing, can anyone explain (or give a link to docs) which servlet is used to "return" the templates html? Is it the Spring dispatcher servlet?


Ok so I had forgotten the @GetMapping, thanks @benjamin c. Adding it however produces:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home", template might not exist or might not be accessible by any of the configured Template Resolvers 

3 Answers 3

1

Please remove the configuration : spring.thymeleaf.prefix=/resources/templates/ By default, this location is the one used by the Thymeleaf engine.

UPDATE: If you want to change the default location (under resources directory), you can use : spring.thymeleaf.prefix=classpath:/templates/

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

Comments

1

Put @RequestMapping("/") before buy function:

@RequestMapping("/") public String buy(Model model) { return "home"; } 

If you have multiple functions in the class, springboot will not know which function will be used for the request. SpringBoot use Thymeleaf is template default. You dont need config for that.

Comments

1

To avoid 404 response, add @GetMapping annotation to buy method.

@GetMapping public String buy(Model model) { return "home"; } 

1 Comment

Yes this resolved the 404, thanks :) I'm getting org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home", template might not exist or might not be accessible by any of the configured Template Resolvers now however.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.