springboot项目集成thymeleaf插件

1.pom文件

 <!-- thymeleaf 模板引用  -->
        <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>

2.application.yml

#配置thymeleaf渲染视图
thymeleaf:
  prefix: classpath:/templates/
  suffix: .html
  cache: true
  enabled: true
  encoding: UTF-8
  mode: HTML

3.resources文件夹下建文件夹

templates

4.controller设置访问路径

@Controller
public class IndexController {


    @GetMapping("/login")
    public String index(){
        return "login";
    }
}

5.login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">//必须加
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Insert title here</title>
</head>
<body>
<p style="color:red">hello world</p>
</body>
</html>

6.需要项目编译,在target目录下生成static和templates文件夹,否则访问页面不生效

问题例如

Error resolving template [login], template might not exist or might not be accessible by any of the

解决方案:
看了网上很多人解决方案都没解决我的 后来发现这个前缀少一个/
spring.thymeleaf.prefix=classpath:/templates/
如果不是这个可以再看看pom文件依赖引入没有,target目录有没有templates

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值