Springboot学习( 八)----Thymeleaf属性

Thymeleaf属性

在html原有的属性上加上了th:,属性的作用不变,但是属性的值由模板引擎进行处理了,在属性中可以使用变量表达式

th:action

定义后台控制器路径,类似<form>标签的action属性,主要结合url表达式,获取动态变量

<form id="login" th:action="@{/login}" th:method="post"></form>

th:method

设置请求方法

<form id="login" th:action="@{/login}" th:method="post"></form>

th:href

定义超链接,主要结合url表达式,获取动态变量

<a th:action="@{/login/student}"></a>

th:src

用于外部资源引入,比如<script>标签的src属性,<img>标签的src属性,常与@{}表达式结合使用,在Springboot项目中的静态资源都放到resources下的static目录下,在static目录下的内容,写路径是不需要写static

<script type="text/javascript" th:src="@{/js/jquery-3.4.1.js}"></script>

th:text

用于显示文本,该属性显示的文本在标签中,如果是文本框,数据会在文本框外显示,想要在文本框内显示,使用th:value

<input type="text "id="login" name="login" th:text="${login}"></input>

th:style

设置样式

<a th:onclick="'funl('+${user.id}+')'" th:style="'color:red'">....</a>

th:each

这个属性非常常用,比如从后台传来一个集合对象,就可以使用此属性进行遍历输出,就像是JSTL中的<c:forEach>,此属性既可以循环遍历集合,也可以循环遍历数组以及map

语法:

在一个html标签中,使用th:each

<div th:each="集合的循环成员,循环的状态变量:${key}">
  <p th:text="${集合的循环成员}"></p>
</div>

//集合的循环成员,循环的状态变量:这是两个自定义的名称
//循环的状态变量 可以不定义,默认的是   集合的循环成员Stat

 循环的状态变量的参数使用:

index:当前迭代对象的index(从0开始计算)

count:当前迭代对象的个数(从1开始计算)

size:被迭代对象的大小(被迭代对象的总个数)

current:当前被迭代的变量

even/odd:布尔值,当前循环是否是奇数/偶数(从0开始计算)

first:布尔值,当前循环是否是第一个

last:布尔值,当前循环是否是最后一个

循环List

在index添加链接:index中的链接就是controller中定义的链接

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学习Thymeleaf模板引擎</title>
</head>
<body>
<a href="text1">标准变量表达式</a>
<br><br><br>
<a href="text2">选择变量表达式</a>
<br><br><br>
<a href="text3">链接表达式</a>
<br><br><br>
<a href="eachList">循环List</a>

</body>
</html>

新建一个controller类

package com.ys.controller;

import com.ys.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class EachController {
    @GetMapping("/eachList")
    public String eachList(Model model){
        List<Student> students=new ArrayList<>();
        students.add(new Student(1,"张三",25) );
        students.add(new Student(2,"李四",26) );
        students.add(new Student(3,"王五",27) );
        students.add(new Student(4,"赵六",21) );
        model.addAttribute("student",students);
        return "eachList";
    }
}

 编写html页面,两种方式显示数据,第一个分段显示每一个数据,第二个是在表格中显示数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http:www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>循环List</title>
</head>
<body>
<div th:each="student,studentStat:${student}">
    <p th:text="${student.id}"></p>
    <p th:text="${student.name}"></p>
    <p th:text="${student.age}"></p>
</div>

<div>
<!--    定义表格-->
    <table border="1" cellpadding="0" cellspacing="0">
<!--        定义表头-->
        <thead>
        <tr>
<!--            定义表头的值-->
            <td>编号</td>
            <td>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值