gpt4 book ai didi

spring-mvc - Spring MVC 和 Gradle(多模块): how refer the/WEB-INF/content how a dependency for a module for Testing

转载 作者:行者123 更新时间:2023-12-03 05:09:49 25 4
gpt4 key购买 nike

我正在与:

  • Spring 框架 - 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小号
  • 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,错误很明显

    我已经完成了以下“解决方案”:
  • Thymeleaf : org.thymeleaf.exceptions.TemplateInputException

  • 但没什么,问题依然存在。

    解决 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com