例如我想給添加一個屬性data-page,可以根據(SpringEL/Ognl)表達式計算獲得。
需求:
期望效果
app個數大於0,有數據時鏈接
<xxx data-page="/app/list">
app個數等於0,沒有數據時鏈接如下
<xxx data-page="/app/create">
解決方法:
切換到Thymeleaf的數據屬性語法,使用data-屬性名稱和連字符-
摘錄自:thymeleaf 3.0.5中文參考手冊
<!DOCTYPE html> <html> <head> <title>Good Thymes Virtual Grocery</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" media="all" href="../../css/gtvg.css" data-th-href="@{/css/gtvg.css}" /> </head> <body> <p data-th-text="#{home.welcome}">Welcome to our grocery store!</p> </body>
示例:
以下是筆者使用的代碼
<dd class="I124_dd_usual I124_dd_usual2 pageChange"data-th-data-page="${user.appCount>0?'/app/list':'/app/create'}">我的應用</dd>
實際生成代碼

