{"id":572,"date":"2026-09-21T18:27:19","date_gmt":"2026-09-21T10:27:19","guid":{"rendered":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/4-thymeleaf-%e7%bb%83%e4%b9%a0%e9%a2%98%e5%8f%82%e8%80%83%e7%ad%94%e6%a1%88-2\/"},"modified":"2026-09-21T18:27:19","modified_gmt":"2026-09-21T10:27:19","slug":"4-thymeleaf-%e7%bb%83%e4%b9%a0%e9%a2%98%e5%8f%82%e8%80%83%e7%ad%94%e6%a1%88-2","status":"publish","type":"post","link":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/4-thymeleaf-%e7%bb%83%e4%b9%a0%e9%a2%98%e5%8f%82%e8%80%83%e7%ad%94%e6%a1%88-2\/","title":{"rendered":"Thymeleaf \u7ec3\u4e60\u9898\u53c2\u8003\u7b54\u6848"},"content":{"rendered":"<h1>Thymeleaf \u7ec3\u4e60\u9898\u53c2\u8003\u7b54\u6848<\/h1>\n<blockquote>\n<p>\u8bf4\u660e\uff1a\u6bcf\u4e2a\u9898\u76ee\u5305\u542bController\u4ee3\u7801\u7247\u6bb5 + html\u9875\u9762\u4ee3\u7801<\/p>\n<\/blockquote>\n<h2>\u9898\u76ee1 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code class=\"language-java\">@GetMapping(\"\/t1\")\npublic String t1(Model model){\n    model.addAttribute(\"username\",\"\u674e\u56db\");\n    model.addAttribute(\"desc\",\"\u6b22\u8fce\u5b66\u4e60Thymeleaf\");\n    return \"t1\";\n}<\/code><\/pre>\n<p>templates\/t1.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;p th:text=\"${username}\"&gt;&lt;\/p&gt;\n    &lt;p&gt;[[${desc}]]&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee2 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t2\")\npublic String t2(Model model){\n    model.addAttribute(\"realName\",\"\u738b\u4e94\");\n    return \"t2\";\n}<\/code><\/pre>\n<p>templates\/t2.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;input type=\"text\" th:value=\"${realName}\"&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee3 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t3\")\npublic String t3(){\n    return \"t3\";\n}<\/code><\/pre>\n<p>templates\/t3.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;a th:href=\"@{\/goods\/view(goodsId=100)}\"&gt;\u5546\u54c1\u67e5\u770b&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee4 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t4\")\npublic String t4(Model model){\n    model.addAttribute(\"score\",75);\n    return \"t4\";\n}<\/code><\/pre>\n<p>templates\/t4.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;p th:if=\"${score &gt;= 60}\"&gt;\u6210\u7ee9\u5408\u683c&lt;\/p&gt;\n    &lt;p th:unless=\"${score &gt;= 60}\"&gt;\u6210\u7ee9\u4e0d\u5408\u683c&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee5 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t5\")\npublic String t5(Model model){\n    model.addAttribute(\"rank\",3);\n    return \"t5\";\n}<\/code><\/pre>\n<p>templates\/t5.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;div th:switch=\"${rank}\"&gt;\n        &lt;p th:case=\"1\"&gt;\u91d1\u724c\u4f1a\u5458&lt;\/p&gt;\n        &lt;p th:case=\"2\"&gt;\u94f6\u724c\u4f1a\u5458&lt;\/p&gt;\n        &lt;p th:case=\"3\"&gt;\u94dc\u724c\u4f1a\u5458&lt;\/p&gt;\n        &lt;p th:case=\"*\"&gt;\u666e\u901a\u6e38\u5ba2&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee6 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t6\")\npublic String t6(Model model){\n    List&lt;String&gt; foodList = Arrays.asList(\"\u7c73\u996d\",\"\u9762\u6761\",\"\u997a\u5b50\");\n    model.addAttribute(\"foodList\",foodList);\n    return \"t6\";\n}<\/code><\/pre>\n<p>templates\/t6.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;ul&gt;\n        &lt;li th:each=\"food,stat : ${foodList}\"&gt;\n            \u5e8f\u53f7[[${stat.count}]]\uff1a[[${food}]]\n        &lt;\/li&gt;\n    &lt;\/ul&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee7 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t7\")\npublic String t7(Model model){\n    model.addAttribute(\"isVip\",true);\n    return \"t7\";\n}<\/code><\/pre>\n<p>templates\/t7.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;head&gt;\n    &lt;style&gt;\n        .vip-style{color:gold;}\n        .normal-style{color:gray;}\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div th:class=\"${isVip} ? 'vip-style' : 'normal-style'\"&gt;\u4f1a\u5458\u4fe1\u606f&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee8 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t8\")\npublic String t8(Model model){\n    model.addAttribute(\"content\",\"&lt;span style='color:red'&gt;\u7ea2\u8272\u6587\u5b57&lt;\/span&gt;\");\n    return \"t8\";\n}<\/code><\/pre>\n<p>templates\/t8.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;div th:utext=\"${content}\"&gt;&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee9 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t9\")\npublic String t9(Model model){\n    String name = \"Thymeleaf\";\n    List&lt;String&gt; foodList = Arrays.asList(\"\u7c73\u996d\",\"\u9762\u6761\",\"\u997a\u5b50\");\n    model.addAttribute(\"name\",name);\n    model.addAttribute(\"foodList\",foodList);\n    return \"t9\";\n}<\/code><\/pre>\n<p>templates\/t9.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;body&gt;\n    &lt;p&gt;\u5b57\u7b26\u4e32\u957f\u5ea6\uff1a&lt;span th:text=\"${#strings.length(name)}\"&gt;&lt;\/span&gt;&lt;\/p&gt;\n    &lt;p&gt;\u96c6\u5408\u603b\u4e2a\u6570\uff1a&lt;span th:text=\"${#lists.size(foodList)}\"&gt;&lt;\/span&gt;&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u9898\u76ee10 \u53c2\u8003\u7b54\u6848<\/h2>\n<p>User\u5b9e\u4f53\u7c7b<\/p>\n<pre><code>public class User {\n    private String name;\n    private Integer age;\n    \/\/\u6784\u9020\u5668\u3001getter setter\n    public User(String name,Integer age){\n        this.name=name;\n        this.age=age;\n    }\n    \/\/get set\n}<\/code><\/pre>\n<p>Controller<\/p>\n<pre><code>@GetMapping(\"\/t10\")\npublic String t10(Model model){\n    List&lt;User&gt; userList = new ArrayList&lt;&gt;();\n    userList.add(new User(\"\u5c0f\u660e\",20));\n    userList.add(new User(\"\u5c0f\u7ea2\",16));\n    model.addAttribute(\"userList\",userList);\n    model.addAttribute(\"tip\",\"\u7528\u6237\u5217\u8868\");\n    return \"t10\";\n}<\/code><\/pre>\n<p>templates\/t10.html<\/p>\n<pre><code>&lt;html xmlns:th=\"[http:\/\/www.thymeleaf.org](http:\/\/www.thymeleaf.org)\"&gt;\n&lt;head&gt;\n    &lt;style&gt;\n        .adult{color:blue}\n        .child{color:orange}\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h3 th:text=\"${tip}\"&gt;&lt;\/h3&gt;\n    &lt;div th:each=\"user,stat : ${userList}\"&gt;\n        &lt;p th:class=\"${user.age&gt;=18} ? 'adult' : 'child'\"&gt;\n            \u5e8f\u53f7[[${stat.count}]]\uff0c\u59d3\u540d\uff1a[[${user.name}]]\uff0c\u5e74\u9f84\uff1a[[${user.age}]]\n        &lt;\/p&gt;\n        &lt;p th:if=\"${user.age&lt;18}\"&gt;\u672a\u6210\u5e74\u7528\u6237&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Thymeleaf \u7ec3\u4e60\u9898\u53c2\u8003\u7b54\u6848 \u8bf4\u660e\uff1a\u6bcf\u4e2a\u9898\u76ee\u5305\u542bController\u4ee3\u7801\u7247\u6bb5 + html\u9875\u9762\u4ee3\u7801 \u9898\u76ee [&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-572","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\/572","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=572"}],"version-history":[{"count":0,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}