当前位置: 首页 > 知识库问答 >
问题:

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

常英毅
2023-03-14
<!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>
<!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>
@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);
    }
}

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>

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

Whitelabel错误页面此应用程序没有针对/Error的显式映射,因此您将其视为一种后退。

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

共有1个答案

顾琛
2023-03-14

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

模板\--片段(文件夹)

  • header.html
  • footer.html
  • example.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>

  //content 
  
</body>
</html>
 类似资料: