Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error resolving template "~{fragments/header", template might not exist or might not be accessible by any of the configured Template Resolvers

Tags:

java

thymeleaf

I just tried leaning spring boot. I use thymeleaf,

list.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>

header.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>

Controller :

@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 file:

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

when i tried open the localhost:8080/users page

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Mar 20 20:51:11 PDT 2018 There was an unexpected error (type=Internal Server Error, status=500). Error resolving template "header", template might not exist or might not be accessible by any of the configured Template Resolvers (users/list:11)

don't know how to add the header and footer

like image 710
Niles Avatar asked Mar 21 '18 04:03

Niles


1 Answers

If your template folder file structure like this.

template \
|
|--fragments(folder)

  • header.html
  • footer.html
  • example.html

(header.html file location is --> /src/main/resources/templates/fragments/header.html)

You can add header section to list.html like this:
(replace head tag )

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

header.html look like this:
(add " th:fragments="header" " to head tag)

<head th:fragments="header" >
    <meta charset="UTF-8" />
    <title>Thymeleaf in action</title>
</head>

If you want to add some "div" section you can add like this.

example.html

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

  // content

</div>

After adding example example.html fragment list.html looks like this :

<!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>
like image 149
Widuranga Dilruksha Avatar answered Sep 18 '22 16:09

Widuranga Dilruksha