`
wx1569567608
  • 浏览: 60375 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

11.从零开始学springboot-整合thymeleaf

 
阅读更多

前言

上一节使用了"过时"的jsp技术,这一节我们来使用springboot推荐的模板thymeleaf技术。

创建项目

使用IDEA创建springboot项目,直接勾选web和thymeleaf依赖 1.png

查看依赖

2.png

添加配置

application.yml:

spring:
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    ##关闭缓存
    cache: false

添加模板

在resources的templates下创建index.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
    <meta charset="utf-8"/>
</head>
<body>
<span th:text="'yes, ' + ${name} + '!'"></span>
</body>
</html>

注意,这里模板内注入了个变量"name",模板都是使用"${变量名}"这种方式注入变量的。

完善

目录结构

3.png

controller/IndexController:

package com.mrcoder.sbthymeleaf.controller;


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

@Controller
public class IndexController {

    @RequestMapping(value = "/")
    public String index(Model model){
        //注入name变量到模板
        model.addAttribute("name", "hello world");
        return "index";
    }
}

访问

4.png

关于thymeleaf的更多语法请自行去看官方文档哦。

项目地址

https://github.com/MrCoderStack/SpringBootDemo/tree/master/sb-thymeleaf

https://gitee.com/MrCoderStack/SpringBootDemo/tree/master/sb-thymeleaf

请关注我的订阅号

订阅号.png

转载于:https://my.oschina.net/u/3066875/blog/3055369

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics