SpringBoot --thymeleaf简单实例

背景:

        这个demo主要是跟随B站的SpringBoot教学视频进行编写的,大致相同,小部分个人添加,详细内容可参考B站视频:尚硅谷SpringBoot顶尖教程(springboot之idea版spring boot)_哔哩哔哩_bilibili

操作:

        先创建一个简单的SpringBoot项目:

可参考:SpringBoot -- idea创建简单的helloWorld项目_DGH2430284817的博客-CSDN博客        

        pom.xml引入thymeleaf依赖包

    <properties>
        <java.version>1.8</java.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
        <!-- thymeleaf2   layout1-->
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    </properties>

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

        创建简单的控制器类(快速开发):

@Controller
public class HelloController {
 
    @RequestMapping("/success")
    public  String success(Map<String , Object> map){
        map.put("name","diaoGH");

        return "success";
    }
}

        创建html文件,放到资源目录下的templates文件夹下(没有就新建)

thymeleaf默认的资源目录为classpath:/templates/

可以从项目依赖包中搜索ThymeleafAutoConfiguration , ThymeleafProperties查看对应配置

 

 创建success.html

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <span>MyName:</span> <div th:text="${name}"></div>

</body>
</html>

运行项目:

输入地址:http://127.0.0.1:8080/success

效果:

 

 

参考代码(码云):

springboot-web-restfulcrud: 简单的springboot项目,简单前段和restful风格的crud

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值