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

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

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

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

場景一

在項目中,有時會遇到下面場景:

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

@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,三目運算符等無法實現(xiàn),目前來說這樣可以實現(xiàn)

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

場景二

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

<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}"
? ? ? ? ?? ?<!-- 加了這個 ‘?' 用于判斷 -->
? ? ? ? ? ? th:selected="${category.id} == ${post?.categoryId}">
? ? ? ? </option>
? ? </select>
</div>
th:selected="${category.id} == ${post?.categoryId}"

當在編輯頁面時,下拉框時需要數(shù)據(jù)填充,并根據(jù)條件選中某一項數(shù)據(jù)

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

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

Thymeleaf基礎(chǔ)語法

一、引用命名空間

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

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

二、常用th標簽

1. th:text

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

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

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

<span th:text="${name}?:'默認值'"></span>

2. th:object

它用于接收后臺傳過來的對象,如以下代碼:

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

3. th:action

它用來指定表單提交地址

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

4. th:value

它用對象將id的值替換為value的屬性

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

5. th:field

它用來綁定后臺對象和表單數(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是通過語法@{…}來處理URL的,需要使用“th:href”和“th:src”等屬性,如以下代碼

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

四、用Thymeleaf進行條件求值

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

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

“th:unless”與“th:if”恰好相反——只有當表達式中的條件不成立時才顯示其內(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會員</p>
? ? <p th:case="*">普通會員</p>
</div>

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

六、Thymeleaf中的字符串替換

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

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

? 或,

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

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

七、Thymeleaf的運算符

1. 算數(shù)運算符

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

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

2. 條件運算符th:if

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

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

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

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

3. 判斷空值

判斷不為空:

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

判斷為空

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

八、Thymeleaf公用對象

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

格式化時間

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

判斷是不是空字符串

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

是否包含(分大小寫)

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

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

相關(guān)文章

最新評論