欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于thymeleaf判斷對(duì)象是否為空的相關(guān)邏輯處理

 更新時(shí)間:2022年10月12日 14:29:15   作者:桐花思雨  
這篇文章主要介紹了關(guān)于thymeleaf判斷對(duì)象是否為空的相關(guān)邏輯處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

thymeleaf 判斷對(duì)象是否為空有關(guān)邏輯

場(chǎng)景一

在項(xiàng)目中,有時(shí)會(huì)遇到下面場(chǎng)景:

添加頁(yè)面和編輯頁(yè)面共用一個(gè)頁(yè)面,而通過后臺(tái)傳來的對(duì)象來判斷提示用戶是編輯頁(yè)面還是添加頁(yè)面,而編輯頁(yè)面要使用這個(gè)對(duì)象的,添加頁(yè)面用不到。在此記錄下自己遇到的問題,看到了別人的博客才解決了

@RequestMapping(path = {"/add", "edit"}, method = {RequestMethod.GET})
public String addOrEdit(Model model, @RequestParam(name = "postId", required = false) Long postId) {
? ? if (!StringUtils.isEmpty(postId)) {
? ? ? ? ? ? UserLoginResult userLoginResult = (UserLoginResult) SecurityUtils.getSubject().getPrincipal();
? ? ? ? ? ? PostVO postVO = postService.findOnePostVO(postId);
? ? ? ? ? ? Assert.isTrue(postVO != null, "該帖子已被刪除");
? ? ? ? ? ? Assert.isTrue(postVO.getUserId().longValue() == userLoginResult.getId().longValue(), "沒有權(quán)限操作");
? ? ? ? ? ? model.addAttribute("post", postVO);
? ? ? ? }
? ? ? ? List<Category> categoryList = categoryService.findCategoryAllOfName();
? ? ? ? model.addAttribute("list", categoryList);
? ? ? ? return "jie/add";
? ? }
}

前后使用了 th:if,th:switch,三目運(yùn)算符等無法實(shí)現(xiàn),目前來說這樣可以實(shí)現(xiàn)

<!-- 正確寫法可以實(shí)現(xiàn) -->
<li class="layui-this" th:text="${post != null?'編輯頁(yè)面':'添加頁(yè)面'}"></li>
<!-- 無法實(shí)現(xiàn) -->
<li class="layui-this" th:text="${post} ne 'null'?'編輯頁(yè)面':'添加頁(yè)面'"></li>

場(chǎng)景二

對(duì)于上述編輯頁(yè)面,要使用后臺(tái)數(shù)據(jù)進(jìn)行下拉框的填充。而添加頁(yè)面無需下拉框數(shù)據(jù)的填充。由于二者是公用一個(gè)頁(yè)面,解決如下,記錄一下

<div class="layui-input-block">
?? ?<select lay-verify="required" name="categoryId" lay-filter="column">
? ? ?? ?<option></option>
? ? ?? ?<!-- 此處遍歷 -->
? ? ? ? <option th:each="category:${categoryList}" th:value="${category.id}"
? ? ? ? ?? ?th:text="${category.categoryName}"
? ? ? ? ?? ?<!-- 加了這個(gè) ‘?' 用于判斷 -->
? ? ? ? ? ? th:selected="${category.id} == ${post?.categoryId}">
? ? ? ? </option>
? ? </select>
</div>
th:selected="${category.id} == ${post?.categoryId}"

當(dāng)在編輯頁(yè)面時(shí),下拉框時(shí)需要數(shù)據(jù)填充,并根據(jù)條件選中某一項(xiàng)數(shù)據(jù)

當(dāng)在添加頁(yè)面時(shí),是不需要數(shù)據(jù)的。此時(shí)就要下拉框取消選中

這個(gè) ? 就是為了判斷對(duì)象是否為空,如果為空就不會(huì)渲染頁(yè)面(下拉框選中)

Thymeleaf基礎(chǔ)語(yǔ)法

一、引用命名空間

要使用Thymeleaf,則需要先加入依賴,然后在模板文件中引用命名空間如下:

<html lang="zh" xmlns:th="http://www.thymeleaf.org">

二、常用th標(biāo)簽

1. th:text

<div th:text="${name}">name</div>

? 它用于顯示控制器傳入的name值

? 如果name不存在,要顯示默認(rèn)值,則使用一下代碼

<span th:text="${name}?:'默認(rèn)值'"></span>

2. th:object

它用于接收后臺(tái)傳過來的對(duì)象,如以下代碼:

<th:obejct="${user}">

3. th:action

它用來指定表單提交地址

<form th:action="@{/article}+${article.id}" method="post"></form>

4. th:value

它用對(duì)象將id的值替換為value的屬性

<input type="text" th:value="${article.id}" name="id" />

5. th:field

它用來綁定后臺(tái)對(duì)象和表單數(shù)據(jù)。Thymeleaf里的“th:field”等同于“th:name”和“th:value”,其具體使用方法見以下代碼

<input type="text" id="title" name="title" th:field="${article.title}" />
<input type="text" id="title" name="title" th:field="*{title}" />

三、Thymeleaf中的URL寫法

Thymeleaf是通過語(yǔ)法@{…}來處理URL的,需要使用“th:href”和“th:src”等屬性,如以下代碼

<a th:href="@{http://eg.com}/" rel="external nofollow" >絕對(duì)路徑</a>
<a th:href="@{/}" rel="external nofollow" >相對(duì)路徑</a>
<a th:href="@{css/bootstrap.min.css}/" rel="external nofollow" >默認(rèn)訪問static下的css文件夾</a>

四、用Thymeleaf進(jìn)行條件求值

Thymeleaf通過“th:if”和“th:unless”屬性進(jìn)行條件判斷。在下面的例子中,<a>標(biāo)簽只有在“th:if”中的條件成立時(shí)才顯示。

<a th:href="@{/login}" rel="external nofollow"  rel="external nofollow"  th:if=${session.user == null}>Login</a>

“th:unless”與“th:if”恰好相反——只有當(dāng)表達(dá)式中的條件不成立時(shí)才顯示其內(nèi)容。在下方代碼中,如果用戶session為空,則不顯示登錄鏈接

<a th:href="@{/login}" rel="external nofollow"  rel="external nofollow"  th:unless=${session.user == null}>Login</a>

五、Switch

Thymeleaf支持Switch結(jié)構(gòu),如以下代碼

<div th:switch="${user.role}">
? ? <p th:case="admin">管理員</p>
? ? <p th:case="vip">vip會(huì)員</p>
? ? <p th:case="*">普通會(huì)員</p>
</div>

上述代碼的意思是:如果用戶角色(role)是admin,則顯示“管理員”;如果用戶角色是vip,則顯示“vip會(huì)員”;如果都不是,則顯示“普通會(huì)員”,即使用“*”表示默認(rèn)情況。

六、Thymeleaf中的字符串替換

有時(shí)需要對(duì)文字中的某一處地方進(jìn)行替換,可以通過字符串拼接操作完成,如以下代碼:

<span th:text="'歡迎您,' + ${name} + '!'"></span>

? 或,

<span th:text="|歡迎您,${name}!|"></span>

? 上面的第2種形式限制比較多,|…|中只能包含變量表達(dá)式${…},不能包含其它常量、條件表達(dá)式等

七、Thymeleaf的運(yùn)算符

1. 算數(shù)運(yùn)算符

如果要在模板中進(jìn)行算數(shù)運(yùn)算,則可以用下面的寫法。以下代碼表示求加和取余運(yùn)算

<span th:text="1+3">1 + 3</span><br/>
<span th:text="9%2">9 % 2</span><br/>

2. 條件運(yùn)算符th:if

下方代碼演示了if判斷,表示:如果從控制器傳來的role值等于"admin",則顯示”歡迎您,管理員“;如果role值等于”vip“,則顯示”歡迎您,vip會(huì)員“

<div th:if="${role} eq admin">
? ? <span>歡迎您,管理員</span>
</div>
<div th:if="${role} eq vip">
? ? <span>歡迎您,vip會(huì)員</span>
</div>

eq是判斷表達(dá)式,代表等于。其它的判斷表達(dá)式如下

  • gt:大于
  • ge:大于或等于
  • eq:等于
  • lt:小于
  • le:小于或等于
  • ne:不等于

3. 判斷空值

判斷不為空:

<sapn th:if="${name} != null">不為空</span>

判斷為空

<sapn th:if="${name} == null">為空</span>

八、Thymeleaf公用對(duì)象

? Thymeleaf還提供了一系列公用(utility)對(duì)象,可以通過”#“直接訪問,如以下用法:

格式化時(shí)間

<td th:text="${#date.format(item.createTime,'yyyy-MM-dd HH:mm:ss')}">格式化時(shí)間</td>

判斷是不是空字符串

<span th:if="${#strings.isEmpty(name)}">空的</span>

是否包含(分大小寫)

<span th:if="${#strings.contains(name,'long')}">包含long</span>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論