Thymeleaf入门

Thymeleaf是前端开发模板,springboot默认支持。前端模板用法大多数是类似的jsp、thymeleaf、vue.js都有while\for\if\switch等使用,页面组件化等。

1.前端模板区别

jsp是前后端完全不分离的,jsp页面写一堆Java逻辑。
thymeleaf好处是html改造方便,可以独立预览。vue.js是完全前后端分离的。

2.快速入门

(1)新建springboot项目

# 关闭Thymeleaf的缓存 spring.thymeleaf.cache=false

(可忽略)可以开启热部署,maven,引入devtool,配置SpringbootApplication,Running Application

Update Policies ,选择 Update resources

准备index.html

th:text ="${name}"

创建Controller

@GetMapping("/index")

String index(Model model){
    model.setAttribute("name","html");

        return "index"
}

3.Thymeleaf语法




    


    
    

测试

运行效果

Thymeleaf入门_第1张图片



3.引入静态文件
th:href="@{/blog/yummy-jekyll/plugins/jquery/dist/jquery.min.js}"
@表示static路径
假设html在templates目录下,

th:href 与 href 的区别
href始终从端口开始作为根路径,如8080/channel/page/add
th:href会寻找项目路径作为根路径,如8080/dx/channel/page/add
4.定义组件及使用
th:fragment="header(title,keywords)"  header是组件名称,后面是传递参数

使用方式: html页面::组件名称(参数)
th:replace="blog/yummy-jekyll/header::header('首页','My Blog')"  

你可能感兴趣的:(html5)