java Spring 和 Thymeleaf:尚未配置模板模式“HTML”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40729897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:26:24  来源:igfitidea点击:

Spring and Thymeleaf: Template mode "HTML" has not been configured

javaspringthymeleaf

提问by asdfgh

I'm trying to figure out the spring pet clinic project : https://github.com/spring-projects/spring-petclinic

我想弄清楚春季宠物诊所项目:https: //github.com/spring-projects/spring-petclinic

Trying to make my own basic spring project, want to use thymeleaf and I don't know what is going on because I'm constantly getting error "Template mode "HTML" has not been configured".

试图制作我自己的基本 spring 项目,想使用 thymeleaf,但我不知道发生了什么,因为我不断收到错误“模板模式“HTML”尚未配置”。

I've been trying to learn this by analysing the pet clinic project, then I started lurking through the tutorial: http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.htmland there is this info about Engines and Resolvers which are not present in pet clinic project.

我一直试图通过分析宠物诊所项目来学习这一点,然后我开始潜伏在教程中:http: //www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html并且有关于引擎和的信息宠物诊所项目中不存在的解析器。

My question: what the hell is going on here and what steps do I need to run thymeleaf in empty spring project?

我的问题:这里到底发生了什么,在空的 spring 项目中运行百里香叶需要哪些步骤?

回答by asdfgh

So I've tried everything I could and it turns out that probably the cause for that was that the default thymeleaf parser is for HTML5 (this means that property spring.thymeleaf.mode in application.properties file equals HTML5).

所以我已经尝试了我能做的一切,结果可能是因为默认的 thymeleaf 解析器是针对 HTML5 的(这意味着 application.properties 文件中的 spring.thymeleaf.mode 属性等于 HTML5)。

Default application.properties values: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

默认 application.properties 值:http: //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

The line "spring.thymeleaf.mode=HTML" from pet clinic project somehow had no effect on that project, but in mine - it did have. Turns out I was trying to switch default parser into HTML, which I didn't have. This resulted in mentioned error. Deleting or commenting this line in application.properties solved the problem.

宠物诊所项目中的“spring.thymeleaf.mode=HTML”行不知何故对该项目没有影响,但在我的项目中 - 它确实有影响。原来我试图将默认解析器切换为 HTML,而我没有。这导致了提到的错误。在 application.properties 中删除或评论这一行解决了这个问题。

回答by dapregala

Looks like you are not using Spring Boot? That github project (pet-clinic) is made using Spring Boot. In spring boot, almost all configurations are abstracted. Meaning there are already default opinionated configurations out of the box, hidden from the code. These include ViewResolver configurations, that's why you can not see it from the codebase.

看起来您没有使用 Spring Boot?那个 github 项目 (pet-clinic) 是使用 Spring Boot 制作的。在 spring boot 中,几乎所有的配置都是抽象的。这意味着已经存在开箱即用的默认自以为是的配置,隐藏在代码中。这些包括 ViewResolver 配置,这就是您无法从代码库中看到它的原因。

回答by Wolfgang Fellenz

I encountered the same message in my spring boot thymeleaf project. Found out that thymeleaf support had been removed in gradle.properties:

我在 spring boot thymeleaf 项目中遇到了同样的消息。发现 thymeleaf 支持已在 gradle.properties 中删除:

### lib versions ###
serviceBaseVersion=0.12.0
springBootVersion=1.5.14.RELEASE
swaggerVersion=2.6.+
thymeleaf.version=3.0.+
thymeleaf-layout-dialect.version=2.1.+

The last two lines were missing. Fixed that and everything was working again.

最后两行不见了。解决了这个问题,一切都恢复了。