SpringBoot 用Thymeleaf模版返回页面出现404
现象
在搭建一个springboot框架前,突然发现访问控制层的json数据等都没问题,debug也能进入控制层的请求方法中,但是在返回到thymeleaf模板页面就报404
@RequestMapping(value = {"/","index"})
public String myIndex() {
return "index";
}
尝试解决
以前也是如此搭建的,是没问题的,于是乎上网查了下,大概有如下两种说法:
第一种:控制层中的方法路径中的index 不能和返回的view的名称一致:尝试改变,问题依然存在
第二种:mavn jar包冲突,需要将本地的mavn仓库清空了,让重新下载jar包:尝试,问题依然存在。
最终解决办法
其实我还是怀疑上面第二种就是我的错,因为我是在父模块下开了多各 模块,之前存在了两个子模块。
为了排除干扰,于是,我单独新建了一个独立的springboot项目,照例配置,竟然成功了。。。
排查发现新生成的springboot的版本是2.4.1,而之前有问题的springboot的版本是2.2.7.RELEASE,
于是修改了sprinboot的版本到2.4.1,发现问题解决了。。。
这个时候觉得应该是springboot的版本和下面的thymeleaf的版本是不是对应出的问题,于是本着再验证的态度,又把springboot的版本号再改回去
结果问题也解决了。。。。。
结论
没有结论,问题解决了,修改了sprigboot的版本到最新的,然后再返回去,就好了。自我感觉应该还是mavenjar包的问题。
附
pom.xml
<?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 https://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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nj</groupId>
<artifactId>springboot-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-security</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
thymeleaf:
prefix: classpath:/templates/
cache: false
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
版权声明:本文为yyj108317原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。