我正在与:
所有项目都基于多模块。
关于
Spring Framework
和 Thymeleaf
整合我有以下@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/view/html/");
templateResolver.setSuffix(".html");
//templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
//In other class
@Bean
public ViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(springTemplateEngine);
return viewResolver;
}
在许多模块中,我有这两个:
thymeleaf-02-controller
: 关于@Controller
小号 thymeleaf-02-web
: 关于.html
文件(css、js 也是)是的,重要的是分开了所有关于
.java
来自所有关于网络文件的文件(.html
、.js
等...)关于
Gradle
他们的配置:在
thymeleaf-02-web
模块存在:apply plugin: 'war'
project(':thymeleaf-02-web') {
description 'Web (HTML, JS, CSS)'
dependencies {
exportedProjects.each{
if("$it"!=":thymeleaf-02-web")
compile project("$it")
}
}
webAppDirName = 'src/main/webapp'
war {
version=''
baseName = warBaseName
}
...
}
在
thymeleaf-02-controller
模块存在:project(':thymeleaf-02-controller') {
description 'Web - Controller'
dependencies {
compile project(':thymeleaf-02-infrastructure')
compile project(':thymeleaf-02-service-api')
...
//Omicron:
//What is the correct approach?
//The code shown below does not work
testCompile project(':thymeleaf-02-web')
testRuntime project(':thymeleaf-02-web')
testRuntime fileTree(dir: "/WEB-INF/view/html", include: '*.html')
}
}
注:如果多模块项目导出到
.war
文件一切正常,当然在 Tomcat
.因此 runtime
环境稳定问题 关于
testing
环境,它只适用于thymeleaf-02-controller
模块。因此总是出现:[main] ERROR org.thymeleaf.TemplateEngine -
[THYMELEAF][main] Exception processing template "persona/deleteOne":
An error happened during template parsing
(template: "ServletContext resource
[/WEB-INF/view/html/persona/deleteOne.html]")
org.thymeleaf.exceptions.TemplateInputException:
An error happened during template parsing
(template: "ServletContext resource
[/WEB-INF/view/html/persona/deleteOne.html]")
...
Caused by: java.io.FileNotFoundException:
Could not open ServletContext resource
[/WEB-INF/view/html/persona/deleteOne.html]
...
由于
FileNotFoundException
,错误很明显我已经完成了以下“解决方案”:
但没什么,问题依然存在。
解决
Omicron
的正确方法是什么?上面显示的部分?因此仅用于测试
thymeleaf-02-controller
模块无法引用/到达/查看/使用所有 .html
位于 /WEB-INF/view/html/..
中的文件来自 thymeleaf-02-web
的目录模块。一件令人好奇的事情是,它必须至少存在一个
.java
src/main/java
中的文件thymeleaf-02-web
中的源文件夹在 thymeleaf-02-controller
中查看的模块模块 thymeleaf-02-web
如何通过 Project and External Dependencies
依赖.否则 thymeleaf-02-web
不怎么出现依赖。
最佳答案
解决方案是应用以下逻辑
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("classpath:/templates/html/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
因此:
templateResolver.setPrefix("/WEB-INF/view/html/");
templateResolver.setPrefix("classpath:/templates/html/");
关于spring-mvc - Spring MVC 和 Gradle(多模块): how refer the/WEB-INF/content how a dependency for a module for Testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930885/