1, 在 application.properties 配置文件中添加 thymeleaf 的配置信息
- spring.datasource.driverClassName=com.MySQL.jdbc.Driver
- spring.datasource.url=jdbc:MySQL://localhost:3306/test
- spring.datasource.username=root
- spring.datasource.password=root
- spring.thymeleaf.mode=html5
- spring.thymeleaf.encoding=UTF-8
- spring.thymeleaf.content-type=text/HTML
- spring.thymeleaf.cache=false
2, 在 pom.xml 中引入 thymeleaf 的 jar 包
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
3, 创建 PageController 并添加 index 方法
- package com.cppdy.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class PageController {
- @RequestMapping("index")
- public String index(Model model) {
- model.addAttribute("name","吹泡泡的魚");
- return "index";
- }
- }
4, 在 src/main/resources 下创建 templates(默认访问此文件下的 HTML), 并创建 index.HTML
在 src/main/resources 下创建 static(默认访问此文件夹下的静态文件), 在 static 文件夹创建 images 文件夹, 并放入一张图片
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <meta charset="UTF-8" />
- <title>
- SpringBoot
- </title>
- </head>
- <body>
- Welcome to cppdy
- <p th:text="${name}">
- </p>
- <img alt="吹泡泡的魚" th:src="@{/images/logo.jpg}" />
- <br/>
- <input th:value="${name}" />
- </body>
- </HTML>
注:@表示当前项目路径, static 文件夹是系统默认加载的静态文件路径
来源: http://www.bubuko.com/infodetail-2862594.html