簡體   English   中英

Thymeleaf 片段不呈現

[英]Thymeleaf fragments don't render

在這一點上,我已經檢查了語句的語法 - 它們看起來是正確的。 改變了程序的結構 - 沒有效果。 即使不同的依賴項也不起作用。 一天半后,是時候尋求幫助了。 這是我的 index.html

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">  
<head th:replace="fragments/layout :: nameOfFrag">
<meta charset="UTF-8">
<title>from index.html</title>
</head>
<body>
    <form action="printName" method="get">
        <label> Enter your name</label>
        <input type="text" name="name"  id="name">
    </form>
    <div th:include="fragments/layout :: body"></div>
</body>
</html>

這是布局:

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">  
<head th:fragment="nameOfFrag">
<meta charset="UTF-8">
<title>this is from layout</title>
</head>
<body>
    <div th:fragment="body">
    <p>This is the body from layout</p>
    </div>
</body>
</html>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>learning_spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learning_spring</name>
    <description>simple_web_dataBase_app</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
    <dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.3.3.RELEASE</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

這是結構

當我在瀏覽器中檢查 html 時,一切似乎都很好,但在 index.html 中沒有任何布局可見

我設法解決了這個問題。 似乎“靜態”文件夾做了什么 - 提供“靜態”內容,這意味着它無法解析任何百里香模板。 將 index.html 移動到 Fragments 文件夾解決了這個問題。 還有一件事 - 為了訪問索引文件,必須通過控制器方法“調用”(返回)它。

暫無
暫無

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

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