1.关于注解
这是老生常谈的话题,也是比较容易犯错的一点,就是我们在控制层中的类上加上了
@RestController注解,这是因为@RestController相当于@Controller和@ResponseBody合在一起的,这时再返回页面也只能是返回的一个字符串了。将@RestController换成@Controller
2.查看是否添加了依赖,并是否配置application.properties里的内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
如果是 application.yml,把application.properties的内容换种写法就行了。
3.现在依赖加了,application也配置了,但运行还是报错
(这时可能报的是template无法找到index页面的500错误)
往pom.xml里再添加代码:
在build标签下的resources标签下添加
<resource>
<directory>src/main/resources</directory>
<includes>
//查找后缀名为properties的文件
<include>**/*.properties</include>
//查找后缀名为xml的文件
<include>**/*.xml</include>
//查找后缀名为html的文件
<include>**/*.html</include>
//查找static文件下的资源文件,防止页面出现后却无法加载CSS,JS等内容的问题
<include>static/**</include>
</includes>
<filtering>false</filtering>
</resource>
相关项目结构如下:
之前自己找解决方法找了好久,大部分的解释都是说是注解的问题,换了注解后还是无法返回到相关页面,现在我把我的解决方法提供一下,希望能帮助到和我一样遇到错误的小伙伴。