Spring Boot使用thymeleaf出现的错误(开发软件IDEA)

在Spring boot项目引用thymeleaf时,总是出现404错误或者500错误。①500错误,②输出字符串,thymeleaf不起作用。③:404错误
①:如果是500错误问题基本上是你映射的路径和方法返回值同名了,使容器在调用方法时分不清。更改一下映射路径就好了。(Ps:500错误不是只有这个问题,还有其他情况也会报500,这时候百度一下看看其他情况即可)。
②:在学习过程中,还有一种情况是直接当成字符串输出到浏览器页面了。这是因为自己的控制类上面使用了ResponseBody+Controller注解。或者使用了ResController组合注解,解决方法就是将控制类上面注解改成Controller即可。
③:重点的404错误。在跟着老师学习过程中,一步一步跟着敲,步骤没有出错一点,但是就是报404错误,这时候就很疑问了,在环境一样,开发工具一样,内容一样的情况是为什么出现这种情况呢。经过反复debug调试,终于找到原因。请求是进去了的,是对返回结果路径错误,才会报404错误。所以就是thymeleaf引入的问题,也就是thymeleaf版本和你SpringBoot版本搭配使用问题,或者和你之前仓库jar包重复或者冲突问题。这里解决办法是:建议重新新建一个仓库,然后再重新在pom文件引入thymeleaf依赖(我就是这样解决的)。下面附上我的pom配置文件代码和yml配置文件代码。(yml配置文件应该是可以不用写的,我找了Jar包源码进去看了,里面配置的一样。不过之前在寻找错误的时候也以为是这方面原因就写了一个yml配置文件去试。)

<?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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot6-30-restfulcrud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot6-30-restfulcrud</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>

下面是yml代码

spring:
  thymeleaf:
    prefix: classpath:/templates/
    cache: false
    suffix: .html
    encoding: UTF-8
    servlet:
      content-type: text/html

终于写完了,开心。希望对你有帮助。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值