SpringBoot中Thymeleaf 快速入门,常用语法

Thymeleaf 快速入门,常用语法

Thymeleaf 是现代化服务器端的Java模板引擎,不同与JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性和兼容性。

1、Maven引入

在 pom.xml 文件引入依赖

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

2、Controller

package com.huven.springboot.comtroller;

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

/**
 * Created by v20 Luna on 2019/11/26 14:54
 */
@Controller
public class UserController {

    @GetMapping("/user")
    public String Hello(Model m){
        m.addAttribute("msg","Hello Thymeleaf");
        return "user";
    }
}

3、html

html 文件在 resource/templates 下面创建

注意: html标签一定要引入 xmlns:th="http://www.thymeleaf.org"

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Hello User</h1>
    <p th:text="${msg}"></p>
</body>
</html>

二、Thymeleaf 语法

1 text value string 操作

我的 Controller

@GetMapping("/test")
public String test(Model m){
    m.addAttribute("msg","Hello");
    return "test";
}

我的 html

<h1>text文字显示</h1>
<p th:text="${msg}"></p>

<h1>value给值</h1>
<input type="text" th:value="${msg}" disabled/>

<h1>string操作</h1>
是否为空:<span th:text="${#strings.isEmpty(msg)}" ></span><br>
是否包含指定的字符串:<span th:text="${#strings.contains(msg,'he')}" ></span><br>
是否以'h'字符串开头:<span th:text="${#strings.startsWith(msg,'h')}" ></span><br>
是否以'o'字符串结尾:<span th:text="${#strings.endsWith(msg,'o')}" ></span><br>
返回字符串的长度:<span th:text="${#strings.length(msg)}" ></span><br>
查找字符串的位置,获取'h'下标:<span th:text="${#strings.indexOf(msg,'h')}" ></span><br>
截取字符串:<span th:text="${#strings.substring(msg,2)}" ></span><br>
截取字符串:<span th:text="${#strings.substring(msg,1,3)}" ></span><br>
字符串转大写:<span th:text="${#strings.toUpperCase(msg)}" ></span><br>
字符串转小写:<span th:text="${#strings.toLowerCase(msg)}" ></span><br>

打开浏览器输出结果

text文字显示
Hello

value给值
string操作

是否为空:false
是否包含指定的字符串:false
是否以'h'字符串开头:false
是否以'o'字符串结尾:true
返回字符串的长度:5
查找字符串的位置,获取'h'下标:-1
截取字符串:llo
截取字符串:el
字符串转大写:HELLO
字符串转小写:hello
2 Date操作

controller

@GetMapping("/date")
public String date(Model m){
    m.addAttribute("date",new Date());
    return "date";
}

html

<h1>Date处理</h1>
格式化日期(浏览器的标准): <span th:text="${#dates.format(date)}" ></span><br>
格式化日期(自定义): <span th:text="${#dates.format(date,'yyyy年MM月dd日 HH时mm分ss秒')}" ></span><br>
日期获取年份: <span th:text="${#dates.year(date)}" ></span><br>
日期获取月份: <span th:text="${#dates.month(date)}" ></span><br>
日期获取日: <span th:text="${#dates.day(date)}" ></span><br>
日期获取小时: <span th:text="${#dates.hour(date)}" ></span><br>
日期获取分钟: <span th:text="${#dates.minute(date)}" ></span><br>
日期获取秒数: <span th:text="${#dates.second(date)}" ></span><br>
日期获取毫秒值: <span th:text="${#dates.millisecond(date)}" ></span><br>

页面输出结果

Date处理

格式化日期(浏览器的标准): 2019年11月26日 下午04时41分27秒
格式化日期(自定义): 2019年11月26日 16时41分27秒
日期获取年份: 2019
日期获取月份: 11
日期获取日: 26
日期获取小时: 16
日期获取分钟: 41
日期获取秒数: 27
日期获取毫秒值: 890
3 if switch使用

controller

@GetMapping("/testIf")
public String testIf(Model m){
    m.addAttribute("age",18);
    m.addAttribute("sex","男");
    return "testIf";
}

html

<h1>If测试</h1>
<span th:if="${age} lt 16">你已经不是青少年了</span><br/>
<span th:if="${age} ge 18">你已经成年了</span><br/>

<h1>switch测试</h1>
<span th:switch="${age}">
    <span th:case="16">16岁</span>
    <span th:case="17">17岁</span>
    <span th:case="18">18岁</span>
    <span th:case="19">19岁</span>
    <span th:case="20">20岁</span>
</span>
Thymeleaf 常用比较符号
比较符号解释
lt小于 <
gt大于 >
eq等于 ==
le小于等于 <=
ge大于等于 >=

页面输出结果

If测试

你已经成年了

switch测试
18岁 
4 each 遍历list中的对象

map集合的话也是一样的

controller

@GetMapping("/each")
public String each(Model m){
    ArrayList<User> list = new ArrayList<User>();
    list.add(new User(1,"小明","男",18));
    list.add(new User(2,"小红","女",17));
    list.add(new User(3,"小花","女",18));
    m.addAttribute("list",list);
    return "each";
}

html

<h1>each遍历</h1>
<p>写法一</p>
<table border="1">
    <tr th:each="user : ${list}">
        <td th:text="${user.getId()}"></td>
        <td th:text="${user.getName()}"></td>
        <td th:text="${user.getSex()}"></td>
        <td th:text="${user.getAge()}"></td>
    </tr>
</table>

<p>写法二</p>
<table border="1">
    <tr th:each="user : ${list}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.sex}"></td>
        <td th:text="${user.age}"></td>
    </tr>
</table>

页面输出:

each遍历

写法一
1 	小明 	男 	18
2 	小红 	女 	17
3 	小花 	女 	18

写法二
1 	小明 	男 	18
2 	小红 	女 	17
3 	小花 	女 	18
5 测试作用域

1、Request

2、Session

3、Attribute

controller

@GetMapping("/area")
public String area(HttpServletRequest request){
    request.setAttribute("req","我是request");
    request.getSession().setAttribute("ses","我是session");
    request.getServletContext().setAttribute("att","我是attribute");
    return "area";
}

html:

<h1> area 测试,三大作用域</h1>
<h2>request</h2>
1<p th:text="${req}"></p>
2<p th:text="${#request.getAttribute('req')}"></p>
3<p th:text="${#httpServletRequest.getAttribute('req')}"></p>

<h2>session</h2>
1<p th:text="${ses}"></p>
2<p th:text="${#httpSession.getAttribute('ses')}"></p>
3<p th:text="${#session.getAttribute('ses')}"></p>

<h2>attribute</h2>
1<p th:text="${att}"></p>
2<p th:text="${#servletContext.getAttribute('att')}"></p>

输出结果:

area 测试,三大作用域

request
1
我是request
2
我是request
3
我是request

session
1

2
我是session
3
我是session

attribute
1
2
我是attribute

注意: 这里发现 <p th:text="${ses}"></p> 是取不到 Session 和 Attribute 对象的

6 URL用法

html:

<h1> URL 的表示</h1>
<a href="http://www.baidu.com">百度-普通html写法</a><br/>
<a th:href="@{http://www.baidu.com}">百度-绝对路径</a><br/>
<p>-------------相对路径----------------</p>
<a th:href="@{/user}">相对路径</a>
<a th:href="@{~/user}">相对服务器的根目录</a>
<a th:href="@{/user(id=6,name=wang)}">相对路径+传参数</a>

实测都可以访问!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值