我正在尝试使用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>
有人可以帮助解决此异常吗?
答案 0 :(得分:0)
您可以显示项目的配置类吗? Thymeleaf找不到您的模板。 documentation表示默认情况下会解析为以下路由:“ / WEB-INF / templates /”,要在其他位置进行查找,您需要覆盖它。我通过使用此类ThymeleafViewResolver来实现这一点。
看看这段代码,我告诉spring应用程序根据我的应用程序上下文查看/ templates /文件夹。
@Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
resolver.setContentType("text/html; charset=UTF-8");
resolver.setCharacterEncoding("UTF-8");
resolver.setViewNames(new String[] {"*.html"});
return resolver;
}
private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setCharacterEncoding("UTF-8");
resolver.setPrefix("/templates/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
这或者评论所建议的
templates.users是具有该名称的文件夹,还是在templates文件夹内的users文件夹?
请检查此内容,因为这可能是导致问题的原因。