Fal Alexandr
Fal Alexandr

Reputation: 149

Spring Boot + JMustache 404 not found error for .html page from /resources/templates folder

So I'm trying to just follow instructions for simple Spring Boot project using devtools+mustache+data-jpa. I'm just copy-pasting the whole thing and it doesn't work, even thought tutorial says "Just press the button and it works". Full source code is here, some listings I will provide in the end.

All I want to do is to redirect to index.html from localhost:8080/ and insert simple value into the template.

But instead:
1. Something redirects me from / to /apex/f?p=4950:1 for some reason
2. If I change mapping to @GetMapping("/home") and try localhost:8080/home I get 404

After enabling logging I found out that PathResourceResolver doesn't scan the /resources/templates directory. And if I add dependency on Thymeleaf, it finds it.

So the question is where is the problem? Should I add some config file? Or Mustache isn't working like that?

IndexController.java

@Controller
public class IndexController {

@GetMapping("/")
public ModelAndView home() {
    Map<String, String> model = new HashMap<>();
    model.put( "name", "Alex" );

    return new ModelAndView( "index", model );
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to Spring, {{ name }}</h1>
</body>
</html>

Dependencies

compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-mustache')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')

Structure

Structure

Log

Log

Upvotes: 2

Views: 3729

Answers (1)

Denis Kozlov
Denis Kozlov

Reputation: 76

In order for given demo app to work, please add following to the main/resources/application.properties

spring.mustache.prefix=classpath:/templates/
spring.mustache.suffix=.html

This will tell Spring where to look for Mustache views and what extension those are supposed to have.

Upvotes: 6

Related Questions