首页 > 解决方案 > thymeleaf-spring4:jar 找不到工件 - gradle intelijj

问题描述

我正在尝试使用带有以下代码的 gradle、groovy 和 spring-boot 框架构建一个简单的应用程序:

@Grab("thymeleaf-spring4")

@Controller
class ViewBasedApp {
    def chapters = ["Quick Start With Groovy",
                    "Quick Start With Java",
                    "Debugging and Managing Your App",
                    "Data Access with Spring Boot",
                    "Securing Your App"]
    @RequestMapping("/")
    def home(@RequestParam(value="name", defaultValue="World") String n) {
        new ModelAndView("home")
                .addObject("name", n)
                .addObject("chapters", chapters)
    }
}

这是我的 build.gradle 文件:

group 'LoginApp'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile("org.springframework.boot:spring-boot-starter-web")
    //compile("org.thymeleaf:thymeleaf-spring4") //first try
   // compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect") //first try
    compile 'org.springframework:spring-webmvc:3.0.0.RELEASE'

    // https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 //secound try
    compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.0.RELEASE'
   // compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '4.1.6.RELEASE' //third try
}

.html 文件:

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Learning Spring Boot - Chapter 1</title>
</head>
<body>
<p th:text="'Hello, ' + ${name}"></p>
<ol>
    <li th:each="chapter : ${chapters}" th:text="${chapter}"></li>
</ol>
</body>
</html>

要启动我的应用程序,我使用 spring boot CLI。当我在命令提示符下使用“spring run”时,我总是遇到同样的错误:在本地(文件:/C:/Users/kubas/repository)中找不到工件:thymeleaf-spring4:jar:。

我试图添加“thymeleaf-spring4.jar” - 从maven页面下载到文件夹“repository”,什么都没有,总是同样的错误。

有人可以就此提出建议吗?

标签: spring-bootgradlegroovythymeleaf

解决方案


我通过添加来自本地存储库的依赖项解决了问题:

buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath("org.thymeleaf:thymeleaf-spring5:3.0.9.RELEASE")
    }
}
repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect") //first try
    compile 'org.springframework:spring-webmvc:4.0.6.RELEASE'
    compile 'org.springframework:spring-web:4.0.6.RELEASE' //@EnableWebMvc @ComponentScan
}

现在 thymeleaf 取自 C:\Users\kubas\org\thymeleaf\thymeleaf-spring5

我不得不将 app.groovy 中的注释更改为@Grab("thymeleaf-spring5")

但现在我无法配置模板文件的路径。我这个文件的路径是: F:\gradle_login_aPP\src\main\resources\templates...

这是错误:

找不到模板位置:类路径:/模板/

我试过添加 application.properties 文件,\src\main\resources\ 但它不起作用。我知道可以添加默认前缀,但我找不到有关文件的信息,我需要在其中添加它。任何建议,如何用 gradle 项目运行这个模板?


推荐阅读