{"id":570,"date":"2026-09-21T18:27:17","date_gmt":"2026-09-21T10:27:17","guid":{"rendered":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/2-thymeleaf-%e5%b8%b8%e7%94%a8%e8%af%ad%e6%b3%95%e5%a4%a7%e5%85%a8-2\/"},"modified":"2026-09-21T18:27:17","modified_gmt":"2026-09-21T10:27:17","slug":"2-thymeleaf-%e5%b8%b8%e7%94%a8%e8%af%ad%e6%b3%95%e5%a4%a7%e5%85%a8-2","status":"publish","type":"post","link":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/2-thymeleaf-%e5%b8%b8%e7%94%a8%e8%af%ad%e6%b3%95%e5%a4%a7%e5%85%a8-2\/","title":{"rendered":"Thymeleaf \u5e38\u7528\u8bed\u6cd5\u5927\u5168"},"content":{"rendered":"<h1>Thymeleaf \u5e38\u7528\u8bed\u6cd5\u5927\u5168<\/h1>\n<blockquote>\n<p>\u9875\u9762\u5fc5\u987b\u6dfb\u52a0\u547d\u540d\u7a7a\u95f4\uff1a<code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;<\/code><\/p>\n<\/blockquote>\n<h2>1. th:text \u6e32\u67d3\u6587\u672c\uff08\u81ea\u52a8\u8f6c\u4e49html\u6807\u7b7e\uff0c\u5b89\u5168\uff09<\/h2>\n<p>\u4f5c\u7528\uff1a\u66ff\u6362\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9<\/p>\n<pre><code class=\"language-html\">&lt;!--\u540e\u7aefmodel.addAttribute(\"msg\",\"&lt;b&gt;\u4f60\u597d&lt;\/b&gt;\");--&gt;\n&lt;p th:text=\"${msg}\"&gt;\u9759\u6001\u9ed8\u8ba4\u6587\u5b57&lt;\/p&gt;\n&lt;!--\u8f93\u51fa\uff1a&amp;lt;b&amp;gt;\u4f60\u597d&amp;lt;\/b&amp;gt;\uff0c\u6807\u7b7e\u4e0d\u4f1a\u52a0\u7c97--&gt;<\/code><\/pre>\n<h2>2. th:utext \u6e32\u67d3\u4e0d\u8f6c\u4e49\u6587\u672c\uff08\u4f1a\u89e3\u6790html\u6807\u7b7e\uff0c\u8c28\u614e\u4f7f\u7528\uff0c\u9632\u6b62XSS\uff09<\/h2>\n<pre><code>&lt;p th:utext=\"${msg}\"&gt;\u9ed8\u8ba4\u6587\u5b57&lt;\/p&gt;\n&lt;!--\u8f93\u51fa\uff1a&lt;b&gt;\u4f60\u597d&lt;\/b&gt;\uff0c\u6587\u5b57\u52a0\u7c97--&gt;<\/code><\/pre>\n<h2>3. th:value \u8868\u5355input\u7684value\u8d4b\u503c<\/h2>\n<pre><code>&lt;!--model.addAttribute(\"name\",\"\u5c0f\u660e\")--&gt;\n&lt;input type=\"text\" th:value=\"${name}\"&gt;<\/code><\/pre>\n<h2>4. th:href \u52a8\u6001\u8d85\u94fe\u63a5<\/h2>\n<pre><code>&lt;!--\u57fa\u7840\u5730\u5740\u62fc\u63a5\u53c2\u6570--&gt;\n&lt;a th:href=\"@{\/user\/detail(id=1,name='test')}\"&gt;\u7528\u6237\u8be6\u60c5&lt;\/a&gt;\n&lt;!--\u751f\u6210 \/user\/detail?id=1&amp;name=test--&gt;<\/code><\/pre>\n<h2>5. th:src \u52a8\u6001\u5f15\u5165\u56fe\u7247\/js\u8d44\u6e90<\/h2>\n<pre><code>&lt;!--model.addAttribute(\"imgPath\",\"\/static\/img\/avatar.jpg\")--&gt;\n&lt;img th:src=\"${imgPath}\" alt=\"\u5934\u50cf\"&gt;<\/code><\/pre>\n<h2>6. th:if \u6761\u4ef6\u5224\u65ad\uff0c\u6ee1\u8db3\u624d\u6e32\u67d3\u8be5\u6807\u7b7e<\/h2>\n<pre><code>&lt;!--model.addAttribute(\"age\",18)--&gt;\n&lt;p th:if=\"${age &gt;= 18}\"&gt;\u6210\u5e74\u4eba&lt;\/p&gt;<\/code><\/pre>\n<h2>7. th:unless \u6761\u4ef6\u5224\u65ad\uff0c<strong>\u4e0d\u6ee1\u8db3\u6761\u4ef6<\/strong>\u624d\u6e32\u67d3\uff08\u7b49\u4ef7\u4e8e !th:if\uff09<\/h2>\n<pre><code>&lt;p th:unless=\"${age &gt;=18}\"&gt;\u672a\u6210\u5e74\u4eba&lt;\/p&gt;<\/code><\/pre>\n<h2>8. th:each \u5faa\u73af\u904d\u5386\uff08\u5217\u8868\/\u6570\u7ec4\uff09<\/h2>\n<p>\u8bed\u6cd5\uff1a<code>th:each=\"\u5355\u9879\u5bf9\u8c61,\u72b6\u6001 : ${\u96c6\u5408}\"<\/code> \u72b6\u6001\u53d8\u91cf\u5185\u7f6e\u5c5e\u6027\uff1aindex(\u4e0b\u6807\u4ece0)\u3001count(\u5e8f\u53f7\u4ece1)\u3001size\u3001first\u3001last<\/p>\n<pre><code>\/\/\u540e\u7aef\nList&lt;String&gt; list = Arrays.asList(\"\u82f9\u679c\",\"\u9999\u8549\",\"\u6a59\u5b50\");\nmodel.addAttribute(\"fruitList\",list);<\/code><\/pre>\n<pre><code>&lt;li th:each=\"fruit,stat : ${fruitList}\"&gt;\n    \u5e8f\u53f7[[${stat.count}]]\uff1a[[${fruit}]]\n&lt;\/li&gt;<\/code><\/pre>\n<h2>9. th:switch \/ th:case \u591a\u5206\u652f\u5224\u65ad<\/h2>\n<pre><code>&lt;!--model.addAttribute(\"level\",2)--&gt;\n&lt;div th:switch=\"${level}\"&gt;\n    &lt;p th:case=\"1\"&gt;\u4e00\u7ea7\u4f1a\u5458&lt;\/p&gt;\n    &lt;p th:case=\"2\"&gt;\u4e8c\u7ea7\u4f1a\u5458&lt;\/p&gt;\n    &lt;p th:case=\"*\"&gt;\u666e\u901a\u7528\u6237&lt;\/p&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<h2>10. th:attr \u6279\u91cf\u8bbe\u7f6e\u6807\u7b7e\u5c5e\u6027<\/h2>\n<pre><code>&lt;div th:attr=\"title=${title},class=${cls}\"&gt;\u6d4b\u8bd5&lt;\/div&gt;<\/code><\/pre>\n<h2>11. th:class \u52a8\u6001\u8bbe\u7f6ecss\u7c7b<\/h2>\n<pre><code>&lt;!--age\u5927\u4e8e\u7b49\u4e8e18\uff0c\u4f7f\u7528red\u6837\u5f0f\uff0c\u5426\u5219blue--&gt;\n&lt;div th:class=\"${age&gt;=18}?'red':'blue'\"&gt;\u6587\u5b57&lt;\/div&gt;<\/code><\/pre>\n<h2>12. th:onclick \u52a8\u6001js\u4e8b\u4ef6<\/h2>\n<pre><code>&lt;button th:onclick=\"alert([[${tip}]])\"&gt;\u70b9\u51fb\u5f39\u7a97&lt;\/button&gt;<\/code><\/pre>\n<h2>13. \u5185\u8054\u8868\u8fbe\u5f0f [[&#8230;]] \u76f4\u63a5\u5199\u5728html\u6587\u672c\u4e2d<\/h2>\n<pre><code>&lt;p&gt;\u7528\u6237\u540d\uff1a[[${username}]]&lt;\/p&gt;<\/code><\/pre>\n<h2>14. \u53d8\u91cf\u8fd0\u7b97<\/h2>\n<p>\u652f\u6301 \u52a0\u51cf\u4e58\u9664\u3001\u6bd4\u8f83\u8fd0\u7b97\uff1a<code>&gt; &lt; &gt;= &lt;= == !=<\/code><\/p>\n<pre><code>&lt;p th:text=\"${age + 2}\"&gt;&lt;\/p&gt;\n&lt;p th:text=\"${age &gt; 20}\"&gt;&lt;\/p&gt;<\/code><\/pre>\n<h2>15. \u5185\u7f6e\u5de5\u5177\u5bf9\u8c61\uff08\u5e38\u7528\uff09<\/h2>\n<ul>\n<li>\n<h1>strings\uff1a\u5b57\u7b26\u4e32\u5de5\u5177<\/h1>\n<\/li>\n<\/ul>\n<pre><code>&lt;p th:text=\"${#strings.length(username)}\"&gt;&lt;\/p&gt;<\/code><\/pre>\n<ul>\n<li>\n<h1>lists\uff1a\u96c6\u5408\u5de5\u5177<\/h1>\n<\/li>\n<\/ul>\n<pre><code>&lt;p th:text=\"${#lists.size(fruitList)}\"&gt;&lt;\/p&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Thymeleaf \u5e38\u7528\u8bed\u6cd5\u5927\u5168 \u9875\u9762\u5fc5\u987b\u6dfb\u52a0\u547d\u540d\u7a7a\u95f4\uff1a&lt;html xmlns:th=&#8221;[http:\/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[26],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-thymeleaf","tag-thymeleaf"],"_links":{"self":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":0,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}