我有以下表達:
<img th:src="@{/api/file/{imageName}(imageName = ${account.profileImageLink})}" alt="profile picture">
我想實作下一個邏輯:如果 imageName 等于 null 所以 imageName = "profile.jpg"
我怎么能通過 Thymleaf 做到這一點?
uj5u.com熱心網友回復:
很多不同的方法來實作這一點。
貓王運算符:
<img th:src="@{/api/file/{imageName}(imageName=${account.profileImageLink} ?: 'profile.jpg')}" alt="profile picture">條件。
<img th:if="${account.profileImageLink != null}" th:src="@{/api/file/{imageName}(imageName = ${account.profileImageLink})}" alt="profile picture"> <img th:unless="${account.profileImageLink != null}" th:src="@{/api/file/profile.jpg}" alt="profile picture">th:帶/三元:
<img th:with="image=${account.profileImageLink == null ? 'profile.jpg' : account.profileImageLink}" th:src="@{/api/file/{imageName}(imageName=${image})}" alt="profile picture">
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/461378.html
