springboot系列一:创建Springboot项目
文章目录
## 1. 创建Maven项目
创建空项目如下:
项目结构如下图:
2、接入springboot依赖
添加parent依赖
<!--1、添加parent依赖begin-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--1、添加parent依赖end-->
添加springboot的web依赖及thymeleaf依赖:
<!--2、spring-boot的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--3、集成thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3. 修改包结构、创建MainApplication入口
4. 写一个Controller返回一个Thymeleaf页面
目录结构如下:
配置文件如下:
application.properties:
server.port=8096
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
Controller如下:
@Controller
@RequestMapping("/demo")
public class SampleController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name","haibo");
return "hello";
}
}
html如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'hello:'+${name}"></p>
</body>
</html>
访问http://localhost:8096/demo/thymeleaf 结果如下: