{"id":569,"date":"2026-09-21T18:27:16","date_gmt":"2026-09-21T10:27:16","guid":{"rendered":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/1-thymeleaf-%e9%85%8d%e7%bd%ae%e4%b8%8e%e4%bd%bf%e7%94%a8%e6%b5%81%e7%a8%8b-2\/"},"modified":"2026-09-21T18:27:16","modified_gmt":"2026-09-21T10:27:16","slug":"1-thymeleaf-%e9%85%8d%e7%bd%ae%e4%b8%8e%e4%bd%bf%e7%94%a8%e6%b5%81%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/www.kirasu.cn\/index.php\/2026\/09\/21\/1-thymeleaf-%e9%85%8d%e7%bd%ae%e4%b8%8e%e4%bd%bf%e7%94%a8%e6%b5%81%e7%a8%8b-2\/","title":{"rendered":"Thymeleaf \u914d\u7f6e\u4e0e\u4f7f\u7528\u6d41\u7a0b"},"content":{"rendered":"<h1>Thymeleaf \u914d\u7f6e\u4e0e\u4f7f\u7528\u6d41\u7a0b<\/h1>\n<blockquote>\n<p>\u57fa\u4e8e SpringBoot 2.x \/ 3.x<\/p>\n<\/blockquote>\n<h2>1. \u5f15\u5165\u4f9d\u8d56\uff08Maven pom.xml\uff09<\/h2>\n<p>SpringBoot\u6574\u5408Thymeleaf\uff0c\u5f15\u5165starter\u4f9d\u8d56\u5373\u53ef<\/p>\n<pre><code class=\"language-xml\">&lt;!-- thymeleaf --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>2. \u9ed8\u8ba4\u76ee\u5f55\u7ea6\u5b9a<\/h2>\n<p>Thymeleaf\u9ed8\u8ba4\u8bfb\u53d6\u8d44\u6e90\u8def\u5f84\uff1a<code>resources\/templates\/<\/code><\/p>\n<ul>\n<li>\u9875\u9762html\u6587\u4ef6\u5168\u90e8\u653e\u5728 templates \u6587\u4ef6\u5939<\/li>\n<li>\n<p>\u9759\u6001\u8d44\u6e90(css\/js\/img)\u653e\u5728 <code>resources\/static\/<\/code><\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1atemplates\u4e0b\u7684\u6587\u4ef6\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7url\u8bbf\u95ee\uff0c\u5fc5\u987b\u7531Controller\u8df3\u8f6c\u6e32\u67d3<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<h2>3. application.yml \u5e38\u7528\u914d\u7f6e\uff08\u53ef\u9009\uff0c\u4e0d\u5199\u4e5f\u6709\u9ed8\u8ba4\u503c\uff09<\/h2>\n<pre><code>spring:\n  thymeleaf:\n    cache: false # \u5f00\u53d1\u65f6\u5173\u95ed\u7f13\u5b58\uff0c\u4fee\u6539\u9875\u9762\u4e0d\u7528\u91cd\u542f\u9879\u76ee\uff1b\u751f\u4ea7\u73af\u5883\u5f00\u542ftrue\n    prefix: classpath:\/templates\/ # \u6a21\u677f\u6587\u4ef6\u524d\u7f00\u8def\u5f84\uff0c\u9ed8\u8ba4\n    suffix: .html # \u6587\u4ef6\u540e\u7f00\uff0c\u9ed8\u8ba4\n    encoding: UTF-8\n    mode: HTML5<\/code><\/pre>\n<h2>4. \u9875\u9762html\u5934\u90e8\u5f15\u5165\u547d\u540d\u7a7a\u95f4<\/h2>\n<pre><code>&lt;html lang=\"zh-CN\" xmlns:th=\"http:\/\/www.thymeleaf.org\"&gt;<\/code><\/pre>\n<h2>5. \u540e\u7aefController\u7f16\u5199<\/h2>\n<pre><code>@Controller\npublic class DemoController {\n    @GetMapping(\"\/index\")\n    public String index(Model model){\n        \/\/ \u628a\u6570\u636e\u5b58\u5165model\uff0c\u4f20\u7ed9\u9875\u9762\n        model.addAttribute(\"username\",\"\u5f20\u4e09\");\n        return \"index\"; \/\/ \u5bf9\u5e94 templates\/index.html\n    }\n}<\/code><\/pre>\n<h2>6. \u9875\u9762\u53d6\u503c\u6e32\u67d3(index.html)<\/h2>\n<pre><code>&lt;body&gt;\n    &lt;p th:text=\"${username}\"&gt;\u9ed8\u8ba4\u6587\u5b57&lt;\/p&gt;\n&lt;\/body&gt;<\/code><\/pre>\n<h2>7. \u8bbf\u95ee\u6d4b\u8bd5<\/h2>\n<p>\u542f\u52a8\u9879\u76ee\uff0c\u8bbf\u95ee <code>[http:\/\/localhost:8080\/index](http:\/\/localhost:8080\/index)<\/code> \u9875\u9762\u4f1a\u6e32\u67d3\u51fa\uff1a<code>\u5f20\u4e09<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thymeleaf \u914d\u7f6e\u4e0e\u4f7f\u7528\u6d41\u7a0b \u57fa\u4e8e SpringBoot 2.x \/ 3.x 1. \u5f15\u5165\u4f9d\u8d56\uff08Maven [&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-569","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\/569","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=569"}],"version-history":[{"count":0,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kirasu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}