尝试使用Thymeleaf处理模板时出错

时间:2018-06-17 15:18:44

标签: java spring spring-boot thymeleaf

早上好! 我正在尝试让Thymeleaf成为模板和片段的一部分,但页面上没有任何反应......

我已经测试了几种配置,但是我无法做任何可以使th:block部分正常工作或正确获取layout.html的内容。

是的,有人能帮帮我吗?请参阅以下代码:

structure

new.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"     
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout">

    <th:block layout:fragment="content">
        <ul>
            <li th:each="name : ${list}">
                <p th:text="${name}"></p>
            </li>
        </ul>
    </th:block>
    </html>

layout.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"    
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="ISO-8859-1" />
    <title>Insert title here</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />
</head>
<body>

    <th:block layout:replace="fragments/header"></th:block>

    <div class="container-fluid">
        <div class="row">
            <th:block layout:fragment="content"></th:block>
        </div>
    </div>

    <script th:src="@{/js/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/js/bootstrap.min.js}"></script>

</body>
</html>

header.html

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#nav">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a href="" class="navbar-brand">Spring App</a>

    <div class="collapse navbar-collapse" id="nav">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a href="" class="nav-link">Home</a>
            </li>
            <li class="nav-item active">
                <a href="" class="nav-link">About</a>
            </li>
        </ul>
    </div>
</nav>

InicioController:

package com.example.teste.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class InicioController {

    @GetMapping("/new")
    public String newPath(Model model) {
            List<String> list = new ArrayList<String>();

        list.add("Teste 1");
        list.add("Teste 2");
        list.add("Teste 3");
        list.add("Teste 4");
        list.add("Teste 5");

        model.addAttribute("list", list);

        return "new";
    }

}

我觉得很奇怪Thymeleaf xmlns没有出现......这是正常的吗?

enter image description here

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>teste</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>teste</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
                <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

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


</project>

我插入另一张显示空白部分的图片,但也没有显示xmlns:th =“http://www.thymeleaf.org”我不知道这是多少正常。

new.html

enter image description here

1 个答案:

答案 0 :(得分:1)

<强>更新

问题是由依赖性问题引起的,当您将以下内容添加到它的pom.xml时:

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

OLD ANSWER

您可能错过了header.html中的片段声明,它应该是这样的:

<div th:fragment="header">    
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#nav">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a href="" class="navbar-brand">Spring App</a>

    <div class="collapse navbar-collapse" id="nav">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a href="" class="nav-link">Home</a>
            </li>
            <li class="nav-item active">
                <a href="" class="nav-link">About</a>
            </li>
        </ul>
    </div>
</nav>
</div>

我还注意到你在layout.html中有与new.html相同的片段声明。我想其中一个需要成为替换标签?