所以我尝试按照使用 devtools+mustache+data-jpa 的简单 Spring Boot 项目的说明进行操作。我只是复制粘贴整个东西,但它不起作用,甚至认为教程说“只要按下按钮就可以了”。完整的源代码是 here ,我将在最后提供一些列表。
我想做的就是从 localhost:8080/ 重定向到 index.html 并在模板中插入简单的值。
而是:
1. 由于某些原因,某些东西将我从/重定向到/apex/f?p=4950:1
2. 如果我将映射更改为 @GetMapping("/home") 并尝试 localhost:8080/home 我得到 404
启用日志记录后,我发现 PathResourceResolver 不扫描/resources/templates 目录。如果我添加对 Thymeleaf 的依赖,它会找到它。
那么问题来了,问题出在哪里?我应该添加一些配置文件吗?或者 Mustache 不是那样工作的?
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 View 以及它们应该具有什么扩展。
关于java - Spring Boot + JMustache 404 未从/resources/templates 文件夹中找到 .html 页面错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886261/