簡體   English   中英

無法使用 thymeleaf 運行 spring boot 應用程序

[英]Unable to run spring boot application with thymeleaf

我的主要應用類:

package com.sopra.springBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {

public static void main(String[] args){
    SpringApplication.run(Application.class, args);
}
}

我的控制器:

    package com.sopra.springBoot;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public String viewHome() {
        return "welcome";
    }
}

我的welcome.html 文件:位於/resources/templates/

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<body>First Spring Boot application

</body>

</html>

我的 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sopra.springBoot</groupId>
  <artifactId>sopra.springBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath/> 
</parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

</dependencies>

<build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我嘗試了互聯網上可用的所有內容,但仍然無法找到模板文件夾並在瀏覽器上收到此錯誤和白標錯誤頁面

2018-01-23 16:08:38.878  WARN 2904 --- [  restartedMain] o.s.b.a.t.ThymeleafAutoConfiguration     : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

請檢查我在哪里做錯了。 仍然無法顯示welcome.html。 關於ThymeleafAutoconfiguration 的任何想法?

我知道這是在第一個答案之后的 3 年。 但是@xingbin 的回答真的幫了我很多。

通過mvn cleanmvn spring-boot:run肯定有效。

但是將上面的代碼添加到 application.properties 中的類路徑對我不起作用。 所以我想通了,我只需要刪除

spring.thymeleaf.prefix=classpath*:/templates/星號 *

這是我的代碼:

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

你的代碼在我的電腦上運行良好。 我認為這是因為您的IDE 無法解析您的類路徑。

IntelliJ IDEA 根據您運行應用程序的方式對類路徑進行不同排序。 通過 main 方法在 IDE 中運行應用程序將導致與使用 Maven 或 Gradle 或從其打包的 jar 運行應用程序時不同的順序。 這可能會導致 Spring Boot 無法在類路徑上找到模板。 如果您受到此問題的影響,您可以在 IDE 中重新排序類路徑以首先放置模塊的類和資源。 或者,您可以配置模板前綴來搜索類路徑上的每個模板目錄:classpath*:/templates/。

這可以通過以下方式解決

  • 將資源文件夾添加到類路徑,請參閱,然后在文件夾資源下創建一個名為 application.properties 的文件,將下面的配置放入其中以包含所有類路徑:

     spring.thymeleaf.prefix=classpath*:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5

或者

  • 通過執行mvn cleanmvn spring-boot:run

默認情況下,Spring Boot 在資源/模板文件夾中尋找百里香葉。 只需將模板文件夾添加到您的資源並將您的 html 文件放入模板中

在此處輸入圖片說明 .

暫無
暫無

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

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