【发布时间】:2019-12-02 00:04:21
【问题描述】:
我正在尝试使用 Thymeleaf 和 Spring-boot。我已经尝试使用我的 helloworld 控制器,它工作正常。但是当我创建一个新的用户控制器并尝试使用 ModelAndView 对象时,我得到一个模板引擎异常,如下所示:
2019-07-23 17:35:43.427 ERROR 22272 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "/users/list": Error resolving template [/users/list], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/users/list], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
这是我的 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.black.spring.boot.eventsharing</groupId>
<artifactId>thymeleaf-demo</artifactId>
<version>1.0.0</version>
<name>thymeleaf-demo</name>
<description>Demo project for Event Sharing</description>
<properties>
<java.version>1.8</java.version>
<thymeleaf-spring5.version>3.0.9.RELEASE</thymeleaf-spring5.version>
<thymeleaf-layout-dialect.version>2.2.0</thymeleaf-layout-dialect.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-test</artifactId>
<scope>test</scope>
</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>
这是我的控制器:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public ModelAndView list(Model model) {
model.addAttribute("userList", userRepository.listUsers());
model.addAttribute("title", "List users");
return new ModelAndView("users/list", "userModel", model);
}
}
这是我的html页面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 Demo!</title>
</head>
<body>
<div th:replace="~{fragment/header :: header}"></div>
<div th:replace="~{fragment/footer :: footer}"></div>
</body>
</html>
谁能帮忙解决这个异常?
【问题讨论】:
标签: java spring spring-boot thymeleaf