{"id":571,"date":"2026-09-21T18:27:18","date_gmt":"2026-09-21T10:27:18","guid":{"rendered":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/3-thymeleaf-%e5%ae%9e%e6%93%8d%e7%bb%83%e4%b9%a0%e9%a2%98-2\/"},"modified":"2026-09-21T18:27:18","modified_gmt":"2026-09-21T10:27:18","slug":"3-thymeleaf-%e5%ae%9e%e6%93%8d%e7%bb%83%e4%b9%a0%e9%a2%98-2","status":"publish","type":"post","link":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/3-thymeleaf-%e5%ae%9e%e6%93%8d%e7%bb%83%e4%b9%a0%e9%a2%98-2\/","title":{"rendered":"Thymeleaf \u5b9e\u64cd\u7ec3\u4e60\u9898"},"content":{"rendered":"<h1>Thymeleaf \u5b9e\u64cd\u7ec3\u4e60\u9898<\/h1>\n<blockquote>\n<p>\u73af\u5883\uff1aSpringBoot + Thymeleaf\uff0c\u5168\u90e8\u9875\u9762\u653e\u5728templates\u76ee\u5f55<br \/>\n\u8981\u6c42\uff1a\u65b0\u5efaController\uff0c\u5f80Model\u5b58\u5165\u5bf9\u5e94\u6570\u636e\uff0c\u7f16\u5199html\u9875\u9762\u5b9e\u73b0\u9700\u6c42\uff1b\u96be\u5ea6\u9010\u5c42\u9012\u8fdb<\/p>\n<\/blockquote>\n<h2>\u9898\u76ee1\u3010\u57fa\u7840\uff1ath:text\u3001\u5185\u8054\u8868\u8fbe\u5f0f\u3011<\/h2>\n<ol>\n<li>\u5728Controller\u5411model\u5b58\u5165\u4e24\u4e2a\u6570\u636e\uff1a<br \/>\nusername = &#8220;\u674e\u56db&#8221;<br \/>\ndesc = &#8220;\u6b22\u8fce\u5b66\u4e60Thymeleaf&#8221;<\/li>\n<li>html\u9875\u9762\u5b9e\u73b0\uff1a\n<ul>\n<li>\u4f7f\u7528 th:text \u5c55\u793ausername<\/li>\n<li>\u4f7f\u7528\u5185\u8054\u8868\u8fbe\u5f0f [[ ]] \u5c55\u793adesc<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:text\u3001\u5185\u8054\u8868\u8fbe\u5f0f<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee2\u3010\u8868\u5355\u3001\u5c5e\u6027\u8d4b\u503c th:value\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165\uff1arealName=&#8221;\u738b\u4e94&#8221;<\/li>\n<li>\u9875\u9762\u7f16\u5199\u4e00\u4e2a\u6587\u672c\u8f93\u5165\u6846\uff0c\u4f7f\u7528th:value\u5c06realName\u4f5c\u4e3ainput\u9ed8\u8ba4\u503c<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:value<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee3\u3010\u52a8\u6001\u94fe\u63a5 th:href\u3011<\/h2>\n<ol>\n<li>\u9875\u9762\u751f\u6210\u8d85\u94fe\u63a5\uff0c\u8bbf\u95ee\u5730\u5740 <code>\/goods\/view<\/code>\uff0c\u643a\u5e26\u53c2\u6570 goodsId=100<\/li>\n<li>\u94fe\u63a5\u6587\u5b57\uff1a\u5546\u54c1\u67e5\u770b<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:href<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee4\u3010\u6761\u4ef6\u5224\u65ad th:if\u3001th:unless\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165 score = 75<\/li>\n<li>\u9875\u9762\u5b9e\u73b0\uff1a\n<ul>\n<li>score &gt;=60\uff1a\u5c55\u793a\u6587\u5b57\u300c\u6210\u7ee9\u5408\u683c\u300d\uff08th:if\uff09<\/li>\n<li>score &lt;60\uff1a\u5c55\u793a\u6587\u5b57\u300c\u6210\u7ee9\u4e0d\u5408\u683c\u300d\uff08th:unless\uff09<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:if\u3001th:unless<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee5\u3010\u591a\u5206\u652f th:switch th:case\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165 rank = 3<\/li>\n<li>\u9875\u9762\u5206\u652f\u5224\u65ad\uff1a\n<pre><code>rank ==1 \u2192 \"\u91d1\u724c\u4f1a\u5458\"\nrank ==2 \u2192 \"\u94f6\u724c\u4f1a\u5458\"\nrank ==3 \u2192 \"\u94dc\u724c\u4f1a\u5458\"\n\u5176\u4ed6\u60c5\u51b5 \u2192 \"\u666e\u901a\u6e38\u5ba2\"<\/code><\/pre>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:switch th:case<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee6\u3010\u5faa\u73af th:each + \u72b6\u6001\u53d8\u91cf\u3011<\/h2>\n<pre><code>List&lt;String&gt; foodList = {\"\u7c73\u996d\",\"\u9762\u6761\",\"\u997a\u5b50\"}<\/code><\/pre>\n<ol>\n<li>Controller\u521b\u5efa\u96c6\u5408\uff0c\u653e\u5165model<\/li>\n<li>\u4f7f\u7528th:each\u5faa\u73af\u904d\u5386\uff0c\u5c55\u793a\u6bcf\u4e00\u9879\u98df\u7269\uff0c\u540c\u65f6\u5c55\u793a\u5e8f\u53f7\uff08\u4ece1\u5f00\u59cb\uff0c\u4f7f\u7528stat.count\uff09<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:each\u3001\u72b6\u6001\u53d8\u91cfstat<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee7\u3010\u52a8\u6001class\u3001\u4e09\u5143\u8fd0\u7b97\u7b26\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165 isVip = true<\/li>\n<li>div\u6807\u7b7e\uff0c\u5f53isVip\u4e3atrue class=&#8221;vip-style&#8221;\uff1b\u5426\u5219 class=&#8221;normal-style&#8221;<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:class\u3001\u4e09\u5143\u8fd0\u7b97<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee8\u3010th:utext \u4e0d\u8f6c\u4e49\u8f93\u51fa\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165 content = &#8220;<span style=\"color:red\">\u7ea2\u8272\u6587\u5b57<\/span>&#8220;<\/li>\n<li>\u4f7f\u7528th:utext\u6e32\u67d3content\uff0c\u8ba9\u9875\u9762\u6587\u5b57\u663e\u793a\u7ea2\u8272<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:utext<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee9\u3010\u5185\u7f6e\u5de5\u5177\u5bf9\u8c61#strings\u3001#lists\u3011<\/h2>\n<ol>\n<li>model\u5b58\u5165 name=&#8221;Thymeleaf&#8221;\uff1bfoodList \u590d\u7528\u9898\u76ee6\u7684\u96c6\u5408<\/li>\n<li>\u9875\u9762\u8f93\u51fa\uff1a\n<ul>\n<li>name\u5b57\u7b26\u4e32\u957f\u5ea6<\/li>\n<li>foodList\u96c6\u5408\u5143\u7d20\u603b\u4e2a\u6570<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1a#strings\uff0c#lists<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u9898\u76ee10\u3010\u7efc\u5408\u5927\u9898\uff0c\u591a\u8bed\u6cd5\u7ec4\u5408\u3011<\/h2>\n<p>\u9700\u6c42\uff1a<\/p>\n<ol>\n<li>model\u5b58\u5165\uff1a<br \/>\nuserList \u96c6\u5408\uff0c\u5305\u542b2\u4e2a\u7528\u6237\u5bf9\u8c61\uff0cUser\u7c7b\u5c5e\u6027 name(String),age(Integer)<br \/>\ntip = &#8220;\u7528\u6237\u5217\u8868&#8221;<\/li>\n<li>\u9875\u9762\u5b9e\u73b0\uff1a\n<ol start=\"3\">\n<li>\u6807\u9898\u4f7f\u7528th:text\u5c55\u793atip<\/li>\n<li>\u5faa\u73af\u904d\u5386userList\uff0c\u8f93\u51fa\u6bcf\u4e2a\u7528\u6237name\u3001age\uff0c\u4ee5\u53ca\u904d\u5386\u5e8f\u53f7stat.count<\/li>\n<li>\u5982\u679cage&gt;=18\uff0c\u7528\u6237\u540d\u5b57\u6dfb\u52a0css\u7c7b adult\uff1b\u5426\u5219\u6dfb\u52a0 child\uff08th:class+\u4e09\u5143\uff09<\/li>\n<li>\u5982\u679cage&lt;18\uff0c\u4f7f\u7528th:if\u989d\u5916\u663e\u793a\u4e00\u884c\u5c0f\u5b57\uff1a\u672a\u6210\u5e74\u7528\u6237<br \/>\n<blockquote>\n<p>\u6d89\u53ca\u8bed\u6cd5\uff1ath:text\u3001th:each\u3001stat\u72b6\u6001\u3001th:class\u4e09\u5143\u3001th:if<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Thymeleaf \u5b9e\u64cd\u7ec3\u4e60\u9898 \u73af\u5883\uff1aSpringBoot + Thymeleaf\uff0c\u5168\u90e8\u9875\u9762\u653e\u5728templat [&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-571","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\/571","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=571"}],"version-history":[{"count":0,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/571\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}