java - Spring Boot + JMustache 404 未从/resources/templates 文件夹中找到 .html 页面错误

标签 java spring spring-boot mustache

所以我尝试按照使用 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')

结构

Structure

日志

Log

最佳答案

为了让给定的演示应用程序正常工作,请将以下内容添加到 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/

相关文章:

java - Spring Boot : Map a map from application. yaml 到带有 @Value 注释的映射

java - 找不到 spring schemalocation

spring-boot - 如何使用 Spring Boot 应用程序在嵌入式 jetty 中配置异步超时

JavaFX:多个选择器修改相同的属性

java - 什么是可以在Eclipse中创建GUI的最佳且免费的Visual Editor?

java - 返回子类

java - 我应该在这种多线程架构下使用 Hibernate 还是放弃它?

spring - 我如何使用带有 jpa 的 spring 调用存储过程

java - spring crudrepository 保存特定的 id

java - 运行 java -jar 时出现 Spring Boot Elastic Beanstalk java.time.DateTimeException