Spring Boot + JMustache 404 未找到 /resources/templates 文件夹中的 .html 页面错误
Spring Boot + JMustache 404 not found error for .html page from /resources/templates folder
所以我尝试按照使用 devtools+mustache+data-jpa 的简单 Spring 引导项目的说明进行操作。我只是复制粘贴整个东西,但它不起作用,甚至认为教程说 "Just press the button and it works"。完整的源代码是here,最后我会提供一些清单。
我想做的就是从 localhost:8080/ 重定向到 index.html 并将简单值插入模板。
而是:
1. 由于某些原因 /apex/f?p=4950:1 将我从 / 重定向到
2. 如果我将映射更改为 @GetMapping("/home") 并尝试 localhost:8080/home 我得到 404
启用日志记录后,我发现 PathResourceResolver 不扫描 /resources/templates 目录。如果我添加对 Thymeleaf 的依赖,它会找到它。
那么问题来了,问题出在哪里?我应该添加一些配置文件吗?或者小胡子不是那样工作的?
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>
依赖关系
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')
结构
日志
为了让给定的演示应用程序正常工作,请将以下内容添加到 main/resources/application.properties
spring.mustache.prefix=classpath:/templates/
spring.mustache.suffix=.html
这将告诉 Spring 在哪里寻找 Mustache 视图以及它们应该具有的扩展名。
所以我尝试按照使用 devtools+mustache+data-jpa 的简单 Spring 引导项目的说明进行操作。我只是复制粘贴整个东西,但它不起作用,甚至认为教程说 "Just press the button and it works"。完整的源代码是here,最后我会提供一些清单。
我想做的就是从 localhost:8080/ 重定向到 index.html 并将简单值插入模板。
而是:
1. 由于某些原因 /apex/f?p=4950:1 将我从 / 重定向到
2. 如果我将映射更改为 @GetMapping("/home") 并尝试 localhost:8080/home 我得到 404
启用日志记录后,我发现 PathResourceResolver 不扫描 /resources/templates 目录。如果我添加对 Thymeleaf 的依赖,它会找到它。
那么问题来了,问题出在哪里?我应该添加一些配置文件吗?或者小胡子不是那样工作的?
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>
依赖关系
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')
结构
日志
为了让给定的演示应用程序正常工作,请将以下内容添加到 main/resources/application.properties
spring.mustache.prefix=classpath:/templates/
spring.mustache.suffix=.html
这将告诉 Spring 在哪里寻找 Mustache 视图以及它们应该具有的扩展名。