本文共 2149 字,大约阅读时间需要 7 分钟。
一、本文概览
二、如何搭建一个SpringBoot+Thymeleaf的项目
链接:
三、语法介绍
1、Thymeleaf的基本框架:
核心就是引入thymeleaf的命名空间:<html xmlns:th="http://www.thymeleaf.org">
Title Hello
2、th:xxx属性
1)、html中的一些属性,比如:id、text、value、href、src分别对应thymeleaf中的th:id、th:text、th:value、th:href、th:src
另外还有一些同属th:xxx但又有点特殊的属性:
2)、th:utext 对应html结点的innerHTML,即值可以是HTML代码。示例:
3)、th:object 用于绑定一个对象,充当结点的上下文。结合例子更好理解:
注:上面总结了th:oject的作用,以及${}与*{}的区别。
4)、th:each 迭代(遍历)
a、数组:
b、List:
c、Set:
d、Map:
3、链接:@{xxx}
作用:配合th:src、th:href等共同发挥作用
注:框架存放和读取静态资源的默认路径是classpath:static(以及其他一些路径)
1)相关结构:
2)、application.properties(如果采用默认配置,可以不需要此文件。这里展示如何自定义静态资源目录)
#框架存放和读取静态资源的默认路径,可自定义#因与默认配置相同,故下行可删spring.web.resources.static-locations=classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/
3)、js.js:
document.getElementById('test').innerText='js链接已生效'
4)、css.css:
body{ color: red;}
5)、test.html:
Title one twobaidu
6)、controller:
package cn.fddlc.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class MyController { @RequestMapping("/test") public String f(Model model){ model.addAttribute("name","Tom"); model.addAttribute("age",20); return "test"; } @RequestMapping("/one/{name}/{age}") @ResponseBody public String one(@PathVariable("name") String n,@PathVariable("age") Integer a){ return "one:"+n+":"+a; } @RequestMapping("/two") @ResponseBody public String two(String name,Integer age){ return "two:"+name+":"+age; }}
7)、结果:
8)、生成的html:
4、读取配置:#{xxx}:
1)、相关结构:
2)、application.properties:
#下面是用于存放properties文件的的默认文件(即classpath:messages.properties或.yml),可自定义。#因与默认配置相同,故下行可删spring.messages.basename=messages#下面的配置与默认配置相同,故可删去spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html
3)、messages.properties:文件位置及名字皆为框架默认,可在application.properties中自定义
name=Tomage=20
4)、直接用#{key}取值即可
转载地址:https://liuxingchang.blog.csdn.net/article/details/116563201 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!