代码之家  ›  专栏  ›  技术社区  ›  Manuel Jordan

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

  •  0
  • Manuel Jordan  · 技术社区  · 6 年前

    我的工作对象是:

    • Spring框架-5.0.4。释放
    • Gradle-4.7
    • 百里香叶-3.0.9。释放

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

    关于 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 s
    • thymeleaf-02-web 当前位置关于 .html 文件(css、js)

    是的,重要的是我们已经分开了 .java 来自所有web文件的文件( .html ,则, .js 等等……)

    关于 Gradle 其配置:

    胸腺素AF-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-控制器 模块存在:

    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-控制器 单元因此总是出现:

    [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-控制器 模块无法引用/访问/查看/使用所有 .html 位于中的文件 /WEB-INF/view/html/.. 目录中的 胸腺素AF-02-web 单元

    有一件事很奇怪,那就是必须至少存在一个 JAVA 中的文件 src/main/java 源文件夹 胸腺素AF-02-web 要在中查看的模块 thymeleaf-02-控制器 模块 胸腺素AF-02-web 如何通过 Project and External Dependencies 否则 胸腺素AF-02-web 不显示如何依赖。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Manuel Jordan    6 年前

    解决方案是应用以下逻辑

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