{"id":573,"date":"2026-09-21T18:27:23","date_gmt":"2026-09-21T10:27:23","guid":{"rendered":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/5-thymeleaf-%e5%bc%80%e5%8f%91%e5%b8%b8%e8%a7%81%e5%9d%91%e7%82%b9-2\/"},"modified":"2026-09-21T18:27:23","modified_gmt":"2026-09-21T10:27:23","slug":"5-thymeleaf-%e5%bc%80%e5%8f%91%e5%b8%b8%e8%a7%81%e5%9d%91%e7%82%b9-2","status":"publish","type":"post","link":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/5-thymeleaf-%e5%bc%80%e5%8f%91%e5%b8%b8%e8%a7%81%e5%9d%91%e7%82%b9-2\/","title":{"rendered":"Thymeleaf \u5f00\u53d1\u5e38\u89c1\u5751\u70b9"},"content":{"rendered":"<h1>Thymeleaf \u5f00\u53d1\u5e38\u89c1\u5751\u70b9<\/h1>\n<blockquote>\n<p>SpringBoot + Thymeleaf \u5f00\u53d1\u4e2d\u9ad8\u9891\u8e29\u5751\u95ee\u9898\u6c47\u603b<\/p>\n<\/blockquote>\n<h2>\u4e00\u3001\u7f13\u5b58\u76f8\u5173\uff08\u6700\u5e38\u9047\u5230\uff09<\/h2>\n<ol>\n<li><strong>\u73b0\u8c61<\/strong>\uff1a\u4fee\u6539html\u9875\u9762\uff0c\u5237\u65b0\u6d4f\u89c8\u5668\u770b\u4e0d\u5230\u53d8\u5316\uff0c\u5fc5\u987b\u91cd\u542f\u9879\u76ee<br \/>\n\u539f\u56e0\uff1aThymeleaf\u9ed8\u8ba4\u5f00\u542f\u6a21\u677f\u7f13\u5b58<br \/>\n\u89e3\u51b3\uff1a\u5f00\u53d1\u73af\u5883\u5728application.yml\u5173\u95ed\u7f13\u5b58<\/p>\n<pre><code class=\"language-yaml\">spring:\nthymeleaf:\ncache: false<\/code><\/pre>\n<\/li>\n<\/ol>\n<blockquote>\n<p>\u26a0\ufe0f\u6ce8\u610f\uff1a\u751f\u4ea7\u73af\u5883\u52a1\u5fc5\u5f00\u542f cache: true\uff0c\u63d0\u5347\u6027\u80fd<\/p>\n<\/blockquote>\n<ol start=\"2\">\n<li>IDEA\u989d\u5916\u5751\uff1a IDEA\u9ed8\u8ba4\u4e0d\u4f1a\u81ea\u52a8\u7f16\u8bd1\u8d44\u6e90\u6587\u4ef6\u3002\u4fee\u6539html\u540e\uff0c\u9700\u8981\u624b\u52a8Build\u9879\u76ee\uff08Ctrl+F9\uff09\u624d\u80fd\u52a0\u8f7d\u6700\u65b0\u9875\u9762\u3002<\/li>\n<\/ol>\n<h2>\u4e8c\u3001\u6587\u4ef6\u8def\u5f84\u95ee\u9898<\/h2>\n<ol>\n<li>templates\u76ee\u5f55\u4e0bhtml<strong>\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee<\/strong> templates\u662f\u670d\u52a1\u7aef\u6e32\u67d3\u76ee\u5f55\uff0c\u6d4f\u89c8\u5668\u4e0d\u80fd\u76f4\u63a5\u8f93\u5165 <code>\/index.html<\/code> \u8bbf\u95ee\uff0c\u5fc5\u987bController\u8fd4\u56de\u89c6\u56fe\u540d\u79f0\u3002 \u9759\u6001\u8d44\u6e90css\u3001js\u3001\u56fe\u7247\u653e\u5728 <code>resources\/static\/<\/code>\uff0c\u624d\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u3002<\/li>\n<li>\n<p>th:href \/ th:href \u8def\u5f84\u5199\u6cd5\u9519\u8bef \u274c \u9519\u8bef\uff1a<code>&lt;a href=\"\/user\/1\"&gt;<\/code> \u786c\u7f16\u7801\uff0c\u4e0d\u63a8\u8350 \u2705 \u6b63\u786e\uff1a<code>&lt;a th:href=\"@{\/user\/1}\"&gt;<\/code><\/p>\n<blockquote>\n<p><code>@{}<\/code> \u4f1a\u81ea\u52a8\u62fc\u63a5\u9879\u76ee\u4e0a\u4e0b\u6587\u8def\u5f84\uff0c\u90e8\u7f72\u9879\u76ee\u4e0d\u5bb9\u6613404<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<h2>\u4e09\u3001\u8f6c\u4e49 th:text \u548c th:utext \u7684\u5b89\u5168\u5751<\/h2>\n<ul>\n<li>th:text\uff1a\u81ea\u52a8\u8f6c\u4e49html\u6807\u7b7e\uff0c<strong>\u9632\u6b62XSS\u653b\u51fb\uff0c\u4f18\u5148\u4f7f\u7528<\/strong><\/li>\n<li>\n<p>th:utext\uff1a\u539f\u6837\u89e3\u6790HTML\uff0c<strong>\u5982\u679c\u6e32\u67d3\u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u6781\u6613XSS\u6f0f\u6d1e<\/strong><\/p>\n<blockquote>\n<p>\u7981\u6b62\u76f4\u63a5\u5c06\u7528\u6237\u63d0\u4ea4\u7684\u6570\u636e\u4f7f\u7528 th:utext \u5c55\u793a\uff01<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<h2>\u56db\u3001\u8868\u8fbe\u5f0f\u8bed\u6cd5\u5751<\/h2>\n<ol>\n<li>\u8868\u8fbe\u5f0f\u5fc5\u987b\u5199 <code>${}<\/code>\uff0c\u6f0f\u5199$\u4f1a\u4e0d\u751f\u6548 \u274c <code>&lt;p th:text=\"username\"&gt;&lt;\/p&gt;<\/code> \u2705 <code>&lt;p th:text=\"${username}\"&gt;&lt;\/p&gt;<\/code><\/li>\n<li>\u6bd4\u8f83\u7b26\u53f7\uff1a<code>&gt;``&lt;<\/code> \u5728html\u4e2d\u4f1a\u89e3\u6790\u6210\u6807\u7b7e\uff0c\u4e24\u79cd\u65b9\u6848<\/li>\n<\/ol>\n<pre><code>&lt;!--\u65b9\u68481\uff1a\u4f7f\u7528\u5185\u7f6e\u522b\u540d --&gt;\nth:if=\"${age ge 18}\"  &gt;=\nth:if=\"${age lt 18}\"  &lt;\nth:if=\"${age le 18}\"  &lt;=\nth:if=\"${age gt 18}\"  &gt;\n\n&lt;!--\u65b9\u68482\uff1a\u8f6c\u4e49\u5199\u6cd5 &amp;gt; &amp;lt; --&gt;\nth:if=\"${age &amp;gt; 18}\"<\/code><\/pre>\n<h2>\u4e94\u3001th:each\u5faa\u73af\u5751<\/h2>\n<ol>\n<li>\u72b6\u6001\u53d8\u91cfstat\u5fc5\u987b\u5b9a\u4e49\uff0c\u5426\u5219\u4e0d\u80fd\u4f7f\u7528stat.count \/ stat.index \u274c <code>&lt;li th:each=\"fruit:${list}\"&gt;<\/code> \u6ca1\u6709stat\u53d8\u91cf \u2705 <code>&lt;li th:each=\"fruit,stat : ${list}\"&gt;<\/code><\/li>\n<li>\u7a7a\u96c6\u5408\u4e0d\u4f1a\u62a5\u9519\uff0c\u9875\u9762\u4e0d\u4f1a\u6e32\u67d3\u4efb\u4f55\u6807\u7b7e\uff1b\u5982\u679c\u60f3\u5c55\u793a\u7a7a\u63d0\u793a\uff0c\u9700\u8981\u642d\u914d <code>#lists.isEmpty()<\/code><\/li>\n<\/ol>\n<pre><code>&lt;p th:if=\"${#lists.isEmpty(fruitList)}\"&gt;\u6682\u65e0\u6570\u636e&lt;\/p&gt;<\/code><\/pre>\n<h2>\u516d\u3001\u5b9e\u4f53\u5bf9\u8c61\u53d6\u503c\u7a7a\u6307\u9488<\/h2>\n<p>\u5f53model\u4e2d\u5bf9\u8c61\u4e3anull\uff0c\u76f4\u63a5\u8bbf\u95ee\u5c5e\u6027\u4f1a\u629b\u5f02\u5e38<\/p>\n<pre><code>&lt;!--\u5982\u679cuser\u4e3anull\uff0c${user.name}\u76f4\u63a5\u62a5\u9519--&gt;\n&lt;p th:text=\"${user.name}\"&gt;&lt;\/p&gt;\n&lt;!--\u5b89\u5168\u5199\u6cd5\uff0c\u4f7f\u7528?\u7a7a\u5b89\u5168\u8fd0\u7b97\u7b26--&gt;\n&lt;p th:text=\"${user?.name}\"&gt;&lt;\/p&gt;<\/code><\/pre>\n<h2>\u4e03\u3001\u547d\u540d\u7a7a\u95f4\u5fd8\u8bb0\u5f15\u5165<\/h2>\n<p>\u9875\u9762html\u5934\u90e8<strong>\u5fc5\u987b\u6dfb\u52a0<\/strong>\u547d\u540d\u7a7a\u95f4\uff0c\u5426\u5219IDEA\u6ca1\u6709\u8bed\u6cd5\u63d0\u793a\uff0c\u90e8\u5206\u5c5e\u6027\u5931\u6548<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;<\/code><\/pre>\n<h2>\u516b\u3001\u4e09\u5143\u8fd0\u7b97\u7b26\u5751<\/h2>\n<p>\u4e09\u5143\u8868\u8fbe\u5f0f\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u5fc5\u987b\u52a0\u5f15\u53f7 \u274c <code>&lt;div th:class=\"${vip} ? vip : normal\"&gt;&lt;\/div&gt;<\/code> \u2705 <code>&lt;div th:class=\"${vip} ? 'vip' : 'normal'\"&gt;&lt;\/div&gt;<\/code><\/p>\n<h2>\u4e5d\u3001\u5185\u7f6e\u5de5\u5177\u5bf9\u8c61\u4f7f\u7528\u6ce8\u610f<\/h2>\n<p>\u5185\u7f6e\u5bf9\u8c61\u4ee5<code>#<\/code>\u5f00\u5934\uff1a<code>#strings<\/code>\u3001<code>#lists<\/code>\u3001<code>#dates<\/code><\/p>\n<blockquote>\n<p>\u5185\u7f6e\u5bf9\u8c61\u53ea\u80fd\u5728Thymeleaf\u8868\u8fbe\u5f0f<code>${}<\/code>\u91cc\u9762\u4f7f\u7528\uff0c\u4e0d\u80fd\u5199\u5728\u666e\u901a\u6587\u672c<\/p>\n<\/blockquote>\n<h2>\u5341\u3001\u5176\u4ed6\u5c0f\u95ee\u9898<\/h2>\n<ol>\n<li>\u4e2d\u6587\u4e71\u7801\uff1a\u914d\u7f6e <code>spring.thymeleaf.encoding=UTF-8<\/code><\/li>\n<li>\u9759\u6001\u9884\u89c8\uff1a\u76f4\u63a5\u53cc\u51fbhtml\u6253\u5f00\u770b\u5230\u7684\u662f\u9ed8\u8ba4\u6587\u672c\uff0c\u53ea\u6709\u540e\u7aef\u6e32\u67d3\u624d\u4f1a\u66ff\u6362${}\u53d8\u91cf\uff0c\u8fd9\u662fthymeleaf\u6b63\u5e38\u7279\u6027\uff0c\u4e0d\u662fbug<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Thymeleaf \u5f00\u53d1\u5e38\u89c1\u5751\u70b9 SpringBoot + Thymeleaf \u5f00\u53d1\u4e2d\u9ad8\u9891\u8e29\u5751\u95ee\u9898\u6c47\u603b \u4e00\u3001\u7f13 [&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-573","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\/573","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=573"}],"version-history":[{"count":0,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}