簡體   English   中英

Spring 啟動 Thymeleaf 不會解析模板

[英]Spring Boot Thymeleaf won't resolve template

我在讓我的 spring 啟動應用程序加載我的簡單網頁時遇到問題。 我已經復制了此處提供的示例並在模板目錄中實現了它們我不斷收到此錯誤

org.thymeleaf.exceptions.TemplateInputException:解析模板[索引]時出錯,模板可能不存在或可能無法被任何配置的模板解析器訪問

下面看一下文件目錄

在此處輸入圖像描述

這就是 controller class 中的所有內容

package com.example.cloud_computing_project;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    private String OpenMainPage() {
        return "index";
    }
}

這是 build.gradle 的樣子

plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', "Hoxton.RC2")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    //implementation 'org.springframework.cloud:spring-cloud-starter-aws-messaging'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

我環顧四周,據我所知,文件是正確的,文件的位置是正確的,指向該位置的 getmapping 是正確的。 我唯一能看到的錯誤是我的 build.gradle 中可能缺少依賴項

任何幫助將不勝感激,謝謝。

我也很努力地處理片段。 如本Baeldung 教程中所述,如果您使用片段,則需要Thymeleaf Spring 5 依賴項 這可能無法解決您當前的錯誤,但可能是下一個錯誤;)

由於您的配置似乎正確(盡管私有端點很有趣),您可以嘗試使用基本 index.html 顯示“Hello Word”而沒有任何片段嗎?

在 Spring 引導 Thymeleaf Starter 2.0 及更高版本中,您需要明確包含 ultraq thymeleaf 布局方言依賴項。 在 Maven 中它將是:

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

我開始好奇是否還有其他方法不起作用。 所以我在 application.properties 文件中包含了“server.port=8081”。 我運行它時沒有這樣做。 考慮到這一點,output 說它在類路徑下找不到模板文件夾:/ 我開始懷疑默認值是否被某些東西覆蓋了。

我根本沒有改變任何配置。 我唯一做的就是配置 build.gradle 以導入依賴項,僅此而已。

我發現一個論壇建議使用 gradle.Tasks.Application.bootRun 運行它

這確實解決了我遇到的問題。 它現在正在查找模板文件夾並加載我的 index.html 文件。 它還使用了 application.properties 文件。 但是,由於這不是由 IntelliJ 構建和運行的,因此它沒有對 output 進行顏色編碼。

根據這個問題,建議的修復是重新導入項目。

我已經將我的項目推送到 git 存儲庫,並且.gitignore 不會推送任何可能導致此問題的文件。 所以我刪除了我的整個工作目錄,然后克隆了推送的存儲庫。

它奏效了 現在一切正常。 application.properties 正在工作,我的 HTML 文件正在 /src/main/resources/templates 下成功加載

還要感謝@MohamedSanaulla 對布局方言依賴的建議。 在我在 gradle 中添加實現之前,我也遇到了麻煩。

使用這個基本的 gradle 配置開始

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-devtools")
    testCompile("junit:junit")
}

使用 ./gradlew build 構建

根據需要對其進行自定義。 如果您仍有問題,請查看https://spring.io/guides/gs/serving-web-content/#scratchhttps://spring.io/guides/gs/gradle/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM