springboot使用thymeleaf模板引擎

1.模板引擎:为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的文档

就是将模板文件和数据通过模板引擎生成一个HTML代码

2.springboot开发中,使用thymeleaf模板引擎,首先在pom文件中的导入依赖

        <!-- 使用thymeleaf模板引擎基于3.x开发,需要导入的依赖-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

3. 使用thymeleaf模板引擎,静态资源必须放在resources/templates

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
// templates目录下的所有页面必须使用@Controller来实现页面跳转
// 需要使用thymeleaf模板引擎支持

@Controller
public class IndexController {
   @RequestMapping("/test")
    public String test(Model model){
       model.addAttribute("msg","hello,springboot");
       return "test";
   }
}

 

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
<!--所有的HTMLy元素都可以被thymeleaf替换接管:   th: 元素名   -->
<div th:text="${msg}"></div>
</body>
</html>

 注意:此项目建议springboot的版本建议不要使用最新版本,否则会出现各种bug

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
 </parent>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值