68. 使用thymeleaf报异常:Not Found, status=404

【从零开始学习SpirngBoot—常见异常汇总】

       我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是抛出了错误信息:

Whitelabel Error Page

This application has no explicit mapping for /error, so you areseeing this as a fallback.

Tue Jul 26 21:01:09 CST 2016

There was anunexpected error (type=Not Found, status=404).

No messageavailable

这个错误有很多方面的原因,博主说说开发过程中博主碰到的情况:

第一种情况:是否忘记引入了thymeleaf依赖,检查pom.xml文件,是否有:

  1. <dependency>
  2.        <groupId>org.springframework.boot</groupId>
  3.        <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

       有一次写例子就给忘了,结果就不可访问了。

第二情况:检查访问地址是否正确?

       不要是访问地址是/demo/hello,结果访问使用了/hello,那么肯定是无法访问的,以及模板文件的目录和hello.html文件是否正确。

这里顺便说明,如果访问的是hello.html,但是hello.html不存在,不是抛出404的错误,而是500的错误,如下信息:

Error resolving template"/hello", template might not exist or might not be accessible by anyof the configured Template Resolvers 

如果看到如上信息,那么就要检查下模板文件是否存在,以及文件名称是否编写正确了。

购买完整视频,请前往:http://www.mark-to-win.com/TeacherV2.html?id=287