文件目录结构如下:
HelloController中的代码如下:
package com.example.demo.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello()
{
return "hello";
}
}
在pom.xml中添加依赖:
<!-- 导入thymeleaf模版 的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
并右键单击pom.xml→Maven→点击 Reimport
在templates文件夹下添加hello.html
代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>springboot-thymeleaf demo</title>
</head>
<body>
good job!!!
</body>
</html>
</html>
点击运行,在网页中查看:
结果: