《Spring Boot实战派》 实例7实现MVC模式的Web应用程序 遇到的问题
Error resolving template [mvcdemo], template might not exist or might not be accessible
by any of the configured Template Resolvers] with root cause
主要原因是模板没找到,说明resource文件可能有问题
第一步:
确保启动文件的位置,这里是MvcDemoApplication,放在最外侧,和子包并列或者处于外侧,这里的子包是contrller和model ,spring-boot会自动加载启动类所在包及其子包下的所有组件
第二步:
需要配置application.properties
文件中视图解析器,笔者这一步出现错误
需要配置成如下
#配置前缀
#spring.thymeleaf.prefix=classpath:/templates/
#配置后缀
#spring.thymeleaf.suffix=.html
Thymeleaf模版默认会使用templates作为视图文件
第三步
注意@Controller中试图模板的名字和resource文件下的html文件名字相匹配。
//定义mvc中的视图模板
ModelAndView modelAndView=new ModelAndView("mvcdemo");//需要和html文件名相匹配
第四步
确保@GetMapping("/mvcdemo")
和控制器url访问路径一致
参考博客SpringBoot—静态页面加载
参考博客异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.