SpringBoot学习笔记(四):Thymeleaf基本使用

Thymeleaf官网
Thymeleaf参考指南

一、Thymeleaf的简单介绍

  • Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。

二、Thymeleaf标准表达式语法

  • 1.变量表达式:${}
  • 2.选择或星号表达式:*{}
  • 3.文字国际化表达式:#{}
  • 4.URL 表达式:@{}

三、pom.xml引入Maven依赖

<!--Thymeleaf模板-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

四、application.properties配置thymeleaf

#关闭thymeleaf缓存
spring.thymeleaf.cache=false
  • html标签
<html lang="en"  xmlns:th="http://www.thymeleaf.org">

五、控制类

  • HelloController类
@Controller
public class HelloController {
    @RequestMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("message", "Hello World!");
        return "hello";
    }
}
  • ExampleController类
@Controller
public class ExampleController {
    @RequestMapping("/string")
    public String string(ModelMap map) {
        map.addAttribute("userName", "Jax");
        return "string";
    }
    @RequestMapping("/if")
    public String ifunless(ModelMap map) {
        map.addAttribute("flag", "yes");
        return "if";
    }
    @RequestMapping("/list")
    public String list(ModelMap map) {
        map.addAttribute("users", getUserList());
        return "list";
    }
    @RequestMapping("/url")
    public String url(ModelMap map) {
        map.addAttribute("type", "more");
        map.addAttribute("img", "https://www.baidu.com/img/flexible/logo/pc/privacy.gif");
        return "url";
    }
    @RequestMapping("/eq")
    public String eq(ModelMap map) {
        map.addAttribute("name", "example");
        map.addAttribute("age", 30);
        map.addAttribute("flag", "yes");
        return "eq";
    }
    @RequestMapping("/switch")
    public String switchcase(ModelMap map) {
        map.addAttribute("sex", "woman");
        return "switch";
    }
    private List<User> getUserList(){
        List<User> list=new ArrayList<User>();
        User user1=new User("Tom",21,"123");
        User user2=new User("Jenny",22,"124");
        User user3=new User("Alice",20,"125");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        return  list;
    }
}

六、template包下的html文件

  • hello.html
<h1 th:text="${message}">Hello World</h1>
  • string.html
<h1>name</h1>
<p th:text="${userName}">example</p>
<span th:text="'Welcome  ' + ${userName} + '!'"></span>
<br/>
<span th:text="|Welcome ${userName}!|"></span>
  • if.html
<h1>If/Unless</h1>
<a th:if="${flag == 'yes'}"  th:href="@{http://www.baidu.com/}"> 百度 </a>
<br/>
<a th:unless="${flag != 'no'}" th:href="@{http://www.runoob.com/}" >菜鸟教程</a>
  • list.html
<tr  th:each="user,iterStat : ${users}">
    <td th:text="${user.name}">example</td>
    <td th:text="${user.age}">6</td>
    <td th:text="${user.pass}">213</td>
    <td th:text="${iterStat.index}">index</td>
</tr>
  • url.html
<h1>URL</h1>
<a  th:href="@{http://www.baidu.com/{type}(type=${type})}">link1</a>
<br/>
<div th:style="'background:url(' + @{${img}} + ');'">
    <br/><br/><br/>
</div>
  • eq.html
<h1>EQ</h1>
<input th:value="${name}"/>
<br/>
<input th:value="${age gt 30 ? '中年':'年轻'}"/>
<br/>
<a th:if="${flag eq 'yes'}"  th:href="@{http://www.runoob.com/}"> 菜鸟 </a>
  • switch.html
<div th:switch="${sex}">
    <p th:case="'woman'">她是女的!</p>
    <p th:case="'man'">他是男的!</p>
    <!-- *: case的默认的选项 -->
    <p th:case="*">不男不女!</p>
</div>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChenBbMing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值