Spring MVC和Gradle(多模块):如何引用/ WEB-INF / content如何依赖模块进行测试

时间:2018-04-19 22:01:59

标签: spring-mvc gradle build.gradle thymeleaf

我正在使用:

  • Spring Framework - 5.0.4.RELEASE
  • Gradle - 4.7
  • Thymeleaf - 3.0.9.RELEASE

所有项目都基于多个模块。

关于Spring FrameworkThymeleaf整合,我有以下内容

@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 s
  • 的全部内容
  • 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文件才能在{ {1}}模块thymeleaf-02-controller如何通过thymeleaf-02-web进行依赖。否则Project and External Dependencies不会出现依赖关系。

1 个答案:

答案 0 :(得分:0)

解决方案应用以下逻辑

@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/");