Spring mvc 带Gradle的Thymeleaf模板解析器前缀
我试图在AWS Elastic Beanstalk中部署一个使用Gradle构建的Spring Boot应用程序,但我一直遇到以下错误:Spring mvc 带Gradle的Thymeleaf模板解析器前缀,spring-mvc,amazon-web-services,spring-boot,gradle,thymeleaf,Spring Mvc,Amazon Web Services,Spring Boot,Gradle,Thymeleaf,我试图在AWS Elastic Beanstalk中部署一个使用Gradle构建的Spring Boot应用程序,但我一直遇到以下错误: 2017-07-05 13:47:17.913 ERROR 2804 --- [nio-5000-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw e
2017-07-05 13:47:17.913 ERROR 2804 --- [nio-5000-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//articulos/index.html]")] with root cause
java.io.FileNotFoundException: class path resource [templates//articulos/index.html] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.thymeleaf.spring4.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring4-3.0.3.RELEASE.jar!/:3.0.3.RELEASE]
...
这是我的配置类:
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
private TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new Java8TimeDialect());
engine.addTemplateResolver(templateResolver());
engine.getConfiguration();
return engine;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public UrlTemplateResolver urlTemplateResolver() {
return new UrlTemplateResolver();
}
生成的文件夹结构如下所示:
我已尝试将TemplateResolver的前缀更改为:
- /模板
- 模板
- 模板/
- /模板/
- 类路径:模板/
- 类路径:/templates/
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.flywaydb:flyway-core')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile ("org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.0.RELEASE")
compile ("org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.0.RELEASE")
compile ("io.github.jpenren:thymeleaf-spring-data-dialect:3.1.1")
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
ext["thymeleaf.version"] = "3.0.3.RELEASE"
这是解包WAR文件的树输出,文件夹“templates”似乎在类路径中:
.
|-- META-INF
|-- WEB-INF
| |-- classes
| | |-- ar
| | | `-- com
| | | `-- reweb
| | | `-- data
| | | `-- repository
| | |-- db
| | | `-- migration
| | |-- static
| | | |-- bootstrap
| | | | |-- css
| | | | |-- fonts
| | | | `-- js
| | | |-- css
| | | |-- img
| | | |-- jquery
| | | | `-- ui
| | | | |-- css
| | | | `-- js
| | | `-- js
| | | `-- articulos
| | `-- templates
| | |-- articulos
| | | |-- aplicaciones
没有运气,知道吗?谢谢需要注意的一件事是,如果Thymeleaf找不到模板,将抛出一个措辞不同的异常: org.thymeleaf.exceptions.TemplateInputException:错误解析 模板“articulos/index”,模板可能不存在或不可用 可由任何已配置的模板解析程序访问 FWIW,您的代码在我的机器上运行良好,只需稍加修改(将TemplateEngine转换为SpringTemplateEngine) 这使我认为您的问题在于模板本身——可能您动态地包含了一个片段(通过
th:replace
otth:include
),该片段试图链接回“/articulos/index.html”,但无法正确解析
我会尝试用最简单的版本替换index.html,如下所示:
<!DOCTYPE html>
<html>
<body>
<div>
Hello world!
</div>
</body>
</html>
你好,世界!
看看它是否被加载
如果这样做有效,请尝试再次检查模板的语法,尤其是双斜杠(//),因为这看起来确实可疑 你还没有发布错误,或者我遗漏了什么?对不起,我忘了,补充道@G00GLEN00B是否可以显示索引页的控制器?错误消息
templates//articulos/index.html
中有双斜杠,这很奇怪。我的控制器返回“articulos/index”,就像StringI一样,我也会尝试classpath:templates
和/或classpath:/templates
。否则,您可以制作一个显示问题的小项目吗?请注意,引发的异常是java.io.FileNotFoundException,并且消息指出该文件不存在,我将尝试使用SprintTemplateEngine