【问题标题】:Spring Boot + JMustache 404 not found error for .html page from /resources/templates folderSpring Boot + JMustache 404 not found error for .html page from /resources/templates 文件夹
【发布时间】:2018-06-16 08:27:36
【问题描述】:

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

结构

日志

【问题讨论】:

    标签: java spring spring-boot mustache


    【解决方案1】:

    为了让给定的演示应用程序正常工作,请将以下内容添加到 main/resources/application.properties

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

    这将告诉 Spring 在哪里寻找 Mustache 视图以及它们应该具有什么扩展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2015-07-31
      • 2020-10-21
      • 2011-11-09
      相关资源
      最近更新 更多