Thymeleaf (三) ---------Thymeleaf 属性


大部分属性和 html 的一样,只不过前面加了一个 th 前缀。 加了 th 前缀的属性,是经过模版引擎处理的

一、th:action 属性

定义后台控制器的路径,类似标签的 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:href="@{/query/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="realName" name="reaName" th:text="${realName}">

六、th:style 属性

设置样式

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

七、th:each 属性

这个属性非常常用,比如从后台传来一个对象集合那么就可以使用此属性遍历输出,它与 JSTL 中的<c: forEach>类似,此属性既可以循环遍历集合,也可以循环遍历数组及 Map。

1、循环列表

List<String> userList = new ArrayList<>();
userList.add(...);
userList.add(...);

model.addAttribute("userList",uerList);
<div th:each="u:${userList}">
	<p th:text="${u.id}"></p>
	<p th:text="${u.name}"></p>
    <p th:text="${u.gender}"></p>
    <p th:text="${u.age}"></p>
</div>

语法说明 :

th:each="user, iterStat : ${userlist}" 中的 ${userList} 是后台传过来的集合

◼ user

定义变量,去接收遍历${userList}集合中的一个数据

◼ iterStat

${userList} 循环体的信息

◼ 其中 user 及 iterStat 自己可以随便取名

◼ interStat 是循环体的信息,通过该变量可以获取如下信息

  • index : 当前迭代对象的 index(从 0 开始计算)
  • count : 当前迭代对象的个数(从 1 开始计算)这两个用的较多
  • size : 被迭代对象的大小
  • current : 当前迭代变量
  • even/odd : 布尔值,当前循环是否是偶数/奇数(从 0 开始计算)
  • first : 布尔值,当前循环是否是第一个
  • last : 布尔值,当前循环是否是最后一个

注意:循环体信息 interStat 也可以不定义,则默认采用迭代变量加上 Stat 后缀,即userStat

2、遍历数组 Array

User[] userArray = new User[3];
user[0] = new User(...);
user[1] = new User(...);
user[2] = new User(...);
model.addAttribute("userArray", userArray);
<tr th:each="u:${userArray}" >
    <td th:text="${uStat.count}+'/'+${uStat.size}"></td>
    <td th:text="${u.id}"></td>
    <td th:text="${u.name}"></td>
    <td th:text="${u.sex}"></td>
    <td th:text="${u.age}"></td>
    <td th:text="${uStat.current.age}"></td>
</tr>

3、遍历 map

Map<String, String> map = new HashMap<>();
map.put("...", "...");
map.put("...", "...");

model.addAttribute("map", map);
<div th:each="m, mapStat:${map}">
	<p th:text="${mapStat.count}"></p>
	<p th:text="${m.key}"></p>
	<p th:text="${m.value}"></p>
     //如果value是对象的话
    <p th:text="${m.value.属性}"></p>
</div>

八、条件判断 if

语法:th:if=”boolean 条件” , 条件为 true 显示体内容,th:unlessth:if 的一个相反操作

<p th:if="${gender == 'male'}">
	性别是 男
</p>

<p th:if="5>0">
    5 > 0
</p>

<p th:if="${age > 50}">
    年龄大于50
</p>

<p th:if="${name}">
    name是空
</p>

九、switch 分支语句

<div th:switch="${gender}">
	<p th:case="m">显示男</p>
	<p th:case="f">显示女</p>
	<p th:case="*">未知</p>
</div>

一旦某个 case 判断值为 true,剩余的 case 则都当做 false," * "表示默认的case,前面的 case 都不匹配时候,执行默认的 case

十、 th:inline 属性

th:inline 有三个取值类型 (text, javascript 和 none)

1、内联 text

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

<div th:inline="text">
    姓名是 [[${name}]]
</div>
<!--不用再加入 th:inline='text'-->
<div>
    性别是 [[${gender}]]
</div>

2、内联 Javascript

可以在 js 中,获取模版中的数据。

<button onclick="fun()">单击按钮</button>
<script type="text/javascript" th:inline="javascript">
	var name = [[${user.name}]];
	var gender = [[${user.gender}]];
	function fun() {
		alert("click 用户是"+name+",他的性别是"+id);
	}
</script>
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
thymeleaf-extras-springsecurity5 是 Thymeleaf 的一个扩展库,提供了更多 Spring Security 相关的 Thymeleaf 实用功能。 使用 thymeleaf-extras-springsecurity5,可以直接在 Thymeleaf 模板中使用 Spring Security 的安全表达式,例如: ```html <div sec:authorize="hasRole('ROLE_ADMIN')">只有具有ROLE_ADMIN角色的用户才能看到这个元素</div> ``` 以上代码中,sec:authorize 属性用于执行 Spring Security 的安全表达式,hasRole 方法用于判断用户是否具有指定角色。 除了 hasRole 方法,还可以使用其他安全表达式,例如: 1. hasAnyRole:判断用户是否具有指定角色中的任意一个,用法如下: ```html <div sec:authorize="hasAnyRole('ROLE_ADMIN','ROLE_USER')">具有ROLE_ADMIN或ROLE_USER角色的用户都能看到这个元素</div> ``` 2. hasAuthority:判断用户是否具有指定权限,用法如下: ```html <div sec:authorize="hasAuthority('USER_READ')">只有具有USER_READ权限的用户才能看到这个元素</div> ``` 3. hasAnyAuthority:判断用户是否具有指定权限中的任意一个,用法如下: ```html <div sec:authorize="hasAnyAuthority('USER_READ','USER_WRITE')">具有USER_READ或USER_WRITE权限的用户都能看到这个元素</div> ``` 在使用 thymeleaf-extras-springsecurity5 时,需要在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> ``` 同时,在 Thymeleaf 模板中需要添加以下命名空间: ```html <html xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> ``` 这样就可以愉快地在 Thymeleaf 模板中使用 Spring Security 的安全表达式了!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

在森林中麋了鹿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值