Thymeleaf在gradle构建中失败

Thymeleaf在gradle构建中失败,gradle,thymeleaf,spring-boot,Gradle,Thymeleaf,Spring Boot,使用Spring Boot和Thymeleaf构建应用程序,IntelliJ中的一切都可以正常工作,但当我通过gradle clean build构建时,就会出现错误 以下是我的目录结构: There is a 1.0.1.RELEASE of Spring Boot now, so you should be using that (and it might just fix your problem). Normally, though, I wouldn't expect you woul

使用Spring Boot和Thymeleaf构建应用程序,IntelliJ中的一切都可以正常工作,但当我通过gradle clean build构建时,就会出现错误

以下是我的目录结构:

There is a 1.0.1.RELEASE of Spring Boot now, so you should be using that (and it might just fix your problem). Normally, though, I wouldn't expect you would need Thymeleaf configuration as long as you put your templates in "classpath:/templates" (those html files are what Thymeleaf calls "templates"). You can specify the template location using 
spring.thymeleaf.prefix
if you want them somewhere else (see docs here).

If you prefer to configure Thmyeleaf yourself completely then the you need to use the conventional bean name for the template resolver ("defaultTemplateResolver"), so that Boot knows what you are doing. Details are in the ThymeleafAutoConfiguration - some of the features there are explicitly called out in the docs, but this one appears not to be.

If you are getting the same exception

Caused by: java.lang.IllegalStateException: Cannot find template location: class path resource [templates/] (please add some templates or check your Thymeleaf configuration)

现在有一个1.0.1.0版本的Spring Boot,所以您应该使用它(它可能会解决您的问题)。不过,通常情况下,只要将模板放在“classpath:/templates”(这些html文件就是Thymeleaf所称的“模板”)中,我就不会期望您需要Thymeleaf配置。如果希望在其他地方使用模板,可以使用
spring.thymeleaf.prefix
指定模板位置(请参阅)


如果您希望自己完全配置Thmeyeraf,那么您需要为模板解析器使用传统的bean名称(“defaultTemplateResolver”),以便引导知道您在做什么。详细信息见-文档中明确列出了其中的一些功能,但这一功能似乎不是。

现在有一个1.0.1.0版本的Spring Boot,所以您应该使用它(它可能会解决您的问题)。不过,通常情况下,只要将模板放在“classpath:/templates”(这些html文件就是Thymeleaf所称的“模板”)中,我就不会期望您需要Thymeleaf配置。如果希望在其他地方使用模板,可以使用
spring.thymeleaf.prefix
指定模板位置(请参阅)


如果您希望自己完全配置Thmeyeraf,那么您需要为模板解析器使用传统的bean名称(“defaultTemplateResolver”),以便引导知道您在做什么。详细信息见-文档中明确列出了其中的一些功能,但这一功能似乎不是。

如果您遇到相同的异常情况

但是使用SpringBoot 1.2.0-RELEASE,您可能只需要创建一个目录src/main/resources/templates


我从SpringBoot 1.1.9升级到1.2.0,并开始出现此异常。添加目录修复了它。

如果您遇到相同的异常

但是使用SpringBoot 1.2.0-RELEASE,您可能只需要创建一个目录src/main/resources/templates


我从SpringBoot 1.1.9升级到1.2.0,并开始出现此异常。添加目录修复了它。

或者,如果您遇到此异常并且不想使用
thymeleaf
,您可以使用

config/application.yml

spring.groovy.template.check-template-location:false
spring的最新版本实际上会在stacktrace中告诉您这一点

原因:java.lang.IllegalStateException:找不到模板位置:classpath:/templates/(请添加一些模板,检查Groovy配置,或设置spring.Groovy.template.check template location=false)

或者,如果您遇到此异常并且不想使用
thymeleaf
,您可以使用

config/application.yml

spring.groovy.template.check-template-location:false
spring的最新版本实际上会在stacktrace中告诉您这一点

原因:java.lang.IllegalStateException:找不到模板位置:classpath:/templates/(请添加一些模板,检查Groovy配置,或设置spring.Groovy.template.check template location=false)

您是否试用过1.0.0.0版本的spring boot?我看到了RCs之间的一些相当大的变化。我自己仍然理解springboot自动配置,但如果您使用StarterWeb,它可能在src/main/resources/templates中查找视图文件。虽然不确定intellij是如何工作的,但可能在intellij中正确地完成了一些额外的配置。请参见:您是否尝试过spring boot的1.0.0.RELEASE版本?我看到了RCs之间的一些相当大的变化。我自己仍然理解springboot自动配置,但如果您使用StarterWeb,它可能在src/main/resources/templates中查找视图文件。虽然不确定intellij是如何工作的,但可能在intellij中正确地完成了一些额外的配置。请看:因此我改为1.0.1.RELEASE,移动了所有html、样式表,并删除了Bean templateResolver和Bean templateEngine。见上面更新的问题。然而,现在没有CSS/JS/图像显示,无论我将它们放在/src/main/resources/下的什么位置。现在,当web资产在web-INF下时,我会收到诸如“NetworkError:404 Not Found-”之类的错误,然后tomcat可以找到它们,但是现在这些资源(不包括html文件)找不到了您是否签出了?我更喜欢“src/main/resources/static”“,但这取决于您。感谢您提供文档链接,我认为它确认了我的资产在正确的位置(/src/main/resources/static/css…)。我没有定义ServletContext,因此我假设这意味着我的html应该在根目录中查找js/css/等,但不管我定义的路径是什么,或者我将资源放在哪里都找不到它们。我用一个无法找到css或javascript的html示例更新了我的问题。链接错误(resources/static/css不作为资源映射存在)。试试/css等等。谢谢,我正在努力理解servlet上下文。一旦我尝试创建一个WAR并部署,这需要改变吗?因此我改为1.0.1.RELEASE,移动了所有html、样式表,并删除了Bean templateResolver和Bean templateEngine。见上面更新的问题。然而,现在没有CSS/JS/图像显示,无论我将它们放在/src/main/resources/下的什么位置。现在,当web资产在web-INF下时,我会收到诸如“NetworkError:404 Not Found-”之类的错误,然后tomcat可以找到它们,但是现在这些资源,除去html文件,都找不到了。您检查了吗?我更喜欢“src/main/resources/static”,但这取决于您