<2>thymeleaf的常见属性

  1. 一般在标签属性前加上th:
th:action
th:method
th:href
th:src
th:id
th:name
th:value
th:attr
th:text
th:object
th:onclick
th:style
th:each
条件判断: th:if、th:unless
内敛表达式: th:inline
  1. th:action为例
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>常见属性</title>
</head>
<body>
<form action="http://localhost:8080/test2">
    用户编号:<input type="text" name="id">
    用户姓名:<input type="text" name="username">
    用户密码:<input type="text" name="pwd">
    <input type="submit" value="提交">
</form>

<form th:action="@{/test2}">
    用户编号:<input type="text" name="id">
    用户姓名:<input type="text" name="username">
    用户密码:<input type="text" name="pwd">
    <input type="submit" value="提交">
</form>
</body>
</html>

th:属性主要用来去后台传到前端的值,如果没有取值可以不用使用

th:each

循环List

  1. Controller测试类
@RequestMapping("/each/list")
    public String eachList(Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setUsername("user" + i);
            user.setAge(18+i);
            users.add(user);
        }
        model.addAttribute("userList", users);
        return "eachList";
    }
  1. 前端展示
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历list集合</title>
</head>
<body>
<h2>循环遍历list集合</h2>
<!--
     user: 当前循环变量名称(随意取名)
     userState: 当前循环变量的状态(可选, 默认是对象变量+Stat)
     ${userList}: 当前循环的集合
     userState.index: 索引从0开始
     userState.count: 计数从1开始
-->
<div th:each="user, userStat:${userList}">
    <span th:text="${userStat.index}"></span>
    <span th:text="${userStat.count}"></span>
    <span th:text="${user.id}"></span>
    <span th:text="${user.username}"></span>
    <span th:text="${user.age}"></span>
</div>
<hr>
<div th:each="user:${userList}">
    <span th:text="${userStat.index}"></span>
    <span th:text="${userStat.count}"></span>
    <span th:text="${user.id}"></span>
    <span th:text="${user.username}"></span>
    <span th:text="${user.age}"></span>
</div>
</body>
</html>

循环Map

  1. Controller测试类
@RequestMapping("/each/map")
    public String eachMap(Model model) {
        Map<Integer, Object> userMaps = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setUsername("user" + i);
            user.setAge(18+i);
            userMaps.put(i, user);
        }
        model.addAttribute("userMaps", userMaps);
        return "eachMap";
    }
  1. 前端展示
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历map集合</title>
</head>
<body>
<h2>循环遍历map集合</h2>
<div th:each="userMap,userMapStat:${userMaps}">
    <span th:text="${userMapStat.index}"></span>
    <span th:text="${userMapStat.count}"></span>
    <span th:text="${userMap.key}"></span>
    <span th:text="${userMap.value}"></span>
    <span th:text="${userMap.value.id}"></span>
    <span th:text="${userMap.value.username}"></span>
    <span th:text="${userMap.value.age}"></span>
</div>
<hr>
<div th:each="userMap:${userMaps}">
    <span th:text="${userMapStat.index}"></span>
    <span th:text="${userMapStat.count}"></span>
    <span th:text="${userMap.key}"></span>
    <span th:text="${userMap.value}"></span>
    <span th:text="${userMap.value.id}"></span>
    <span th:text="${userMap.value.username}"></span>
    <span th:text="${userMap.value.age}"></span>
</div>
</body>
</html>

循环Array(与List一样)

  1. Controller测试类
@RequestMapping("/each/array")
    public String eachArray(Model model) {
        User[] userArray = new User[10];
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setUsername("user" + i);
            user.setAge(18+i);
            userArray[i] = user;
        }
        model.addAttribute("userArray", userArray);
        return "eachArray";
    }
  1. 前端展示
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环遍历array集合</title>
</head>
<body>
<h2>循环遍历array集合</h2>
<!--
     user: 当前循环变量名称(随意取名)
     userState: 当前循环变量的状态(可选, 默认是对象变量+Stat)
     ${userList}: 当前循环的集合
     userState.index: 索引从0开始
     userState.count: 计数从1开始
-->
<div th:each="user, userStat:${userArray}">
    <span th:text="${userStat.index}"></span>
    <span th:text="${userStat.count}"></span>
    <span th:text="${user.id}"></span>
    <span th:text="${user.username}"></span>
    <span th:text="${user.age}"></span>
</div>
<hr>
<div th:each="user:${userArray}">
    <span th:text="${userStat.index}"></span>
    <span th:text="${userStat.count}"></span>
    <span th:text="${user.id}"></span>
    <span th:text="${user.username}"></span>
    <span th:text="${user.age}"></span>
</div>
</body>
</html>

条件判断

  1. Controller测试类
@Controller
public class IfController {
    @RequestMapping("/condition")
    public String condition(Model model) {
        model.addAttribute("sex", 1);
        model.addAttribute("flag", true);
        model.addAttribute("productType", 0);
        return "condition";
    }
}
  1. 前档展示
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>条件判断</title>
</head>
<body>
<h1>th:if 满足条件执行</h1>
<div th:if="${sex eq 1}"></div>
<div th:if="${sex eq 0}"></div>

<h1>th:unless 与th:if用法相反</h1>
<div th:unless="${sex ne 1}"></div>

<h1>th:switch</h1>
<div th:switch="${productType}">
    <span th:case="0">产品0</span>
    <span th:case="1">产品1</span>
    <span th:case="*">其他</span>
</div>
</body>
</html>

内敛表达式th:inline

内敛表达式取值不会依赖html标签

内敛文本th:inline=“text”

内敛文本表达式不依赖于html标签,直接使用内敛表达式[[表达式]]即可获取动态数据,但必须要求在父级标签上加 th:inline =“text”属性

内敛脚本th:inline=“javascript”

script不能使用${data}直接去后台的数据,要求在 script 标签上加 th:inline =“text”属性,使用[[表达式]]取值

  • 测试页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>内敛表达式</title>
</head>
<body>
<div th:text="${data}">xxxx</div>

<h2>内敛文本 th:inline="text"</h2>
<div th:inline="text">
    数据:[[${data}]]
</div>

<h2>内敛脚本 th:inline="javascript"</h2>
<button onclick="show()">展示数据</button>
<script type="application/javascript" th:inline="javascript">
    function show() {
        alert([[${data}]])
    }
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值