从/resources/templates文件夹中找不到.html页面的Spring Boot + JMustache 404错误 [英] Spring Boot + JMustache 404 not found error for .html page from /resources/templates folder
问题描述
因此,我尝试仅使用devtools + mustache + data-jpa遵循简单Spring Boot项目的说明.我只是复制粘贴整个内容,但是它不起作用,甚至以为教程说只要按一下按钮就可以了".完整的源代码是此处,最后我将提供一些清单.
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.
我要做的就是从 localhost:8080/重定向到 index.html 并将简单值插入模板.
All I want to do is to redirect to index.html from localhost:8080/ and insert simple value into the template.
但相反:
1.出于某种原因,某人将我从/重定向到/apex/f?p = 4950:1
2.如果我将映射更改为@GetMapping("/home")并尝试 localhost:8080/home ,我得到404
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
启用日志记录后,我发现PathResourceResolver不会扫描/resources/templates目录.而且,如果我添加对Thymeleaf的依赖关系,它就会找到它.
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>
依赖项
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
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
这将告诉Spring在哪里寻找Mustache视图以及应该具有的扩展名.
This will tell Spring where to look for Mustache views and what extension those are supposed to have.
这篇关于从/resources/templates文件夹中找不到.html页面的Spring Boot + JMustache 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!