Spring Boot中Thymeleaf模板引擎的使用

Spring Boot中Thymeleaf模板引擎的使用

一.什么是Thymeleaf引擎

Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。SpringBoot默认使用的就是themeleaf模板引擎。

创建工程

1.创建Spring Boot工程,pom.xml文件中加入Thymeleaf的依赖

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.在application.propertise文档中配置thymeleaf信息

spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5

3.写控制器

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserContrller {

    @RequestMapping("/test")
    public String test(){
        System.out.println("hello...");
        return "hello";
    }

}

这边注意是@Controller注解,不是@RestController。这两者区别可以参考
@Controller和@RestController区别

4.写html
templates文件夹下创建hello.html

<body>
   <h2>hello spring boot</h2>
</body>

5.启动项目,测试
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值