解析模板“~{fragments/header”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问

Nil*_*les 6 java thymeleaf

我刚试过倾斜弹簧靴。我用百里香,

列表.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      >
<head>
    <meta charset="UTF-8" />
    <title>Thymeleaf in action</title>
</head>
<body>

<div th:replace="~{fragments/header}:: header"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

头文件.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      >
<head>
    <meta charset="UTF-8" />
    <title>Thymeleaf in action</title>
</head>
<body>
<div th:fragment="header">
    header
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

控制器 :

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping
    public ModelAndView list(Model model) {
        model.addAttribute("userList", userRepository.listUsers());
        model.addAttribute("title", "account management");
        return new ModelAndView("users/list", "userModel", model);
    }
}
Run Code Online (Sandbox Code Playgroud)

pom文件:

<?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>Blog</groupId>
    <artifactId>Blog</artifactId>
    <version>1.0-SNAPSHOT</version>


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


    <dependencies>
        <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>

    </dependencies>



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



</project>
Run Code Online (Sandbox Code Playgroud)

当我尝试打开 localhost:8080/users 页面时

白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。

Tue Mar 20 20:51:11 PDT 2018 出现意外错误(类型 = 内部服务器错误,状态 = 500)。解析模板“标题”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问(用户/列表:11)

不知道如何添加页眉和页脚

小智 5

如果你的模板文件夹文件结构是这样的。

模板 \
|
|--fragments(文件夹)

  • 标题.html
  • 页脚.html
  • 示例.html

(header.html 文件位置是 --> /src/main/resources/templates/fragments/header.html)

您可以像这样将标题部分添加到 list.html :(
替换 head 标记)

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

<body>

  //content 
  
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

header.html 看起来像这样:(
添加“ th:fragments="header" " 到 head 标签)

<head th:fragments="header" >
    <meta charset="UTF-8" />
    <title>Thymeleaf in action</title>
</head>
Run Code Online (Sandbox Code Playgroud)

如果你想添加一些“div”部分,你可以像这样添加。

示例.html

<div th:replace="fragments/sample">

  // content

</div>
Run Code Online (Sandbox Code Playgroud)

添加示例 example.html 片段 list.html 后如下所示:

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

<body>

  <div th:replace="fragments/example"> </div>
  
</body>
</html>
Run Code Online (Sandbox Code Playgroud)