【发布时间】:2018-02-14 02:17:21
【问题描述】:
我尝试创建一个简单的 Spring Boot 应用程序
我创建了spring boot应用类、配置类、控制器类和index.html。
我添加了 Thymeleaf 依赖并将 html 页面放在资源文件夹下 (\src\main\resources\templates\index.html)
但是当我运行应用程序时,它给出了一个错误
org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers
请给我一个解决方案。
@SpringBootApplication
@ComponentScan(basePackages = {"com.mail"})
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
控制器类
@RestController
public class IndexController {
@RequestMapping(value="/", method = RequestMethod.GET)
String index(){
System.out.println("..............hit");
return "index";
}
Thymeleaf 的 Web 配置
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
index.html 页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Spring Framework</title>
</head>
<body>
<h1> Hello Spring Boot </h1>
</body>
</html>
【问题讨论】:
标签: java spring-boot thymeleaf