【springboot系列】创建springboot项目

本文是springboot系列的第一部分,主要介绍了如何创建Springboot项目,包括接入springboot依赖,修改包结构,创建MainApplication入口,编写返回Thymeleaf页面的Controller。通过这些步骤,读者可以实现一个简单的Springboot应用并运行。
摘要由CSDN通过智能技术生成

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 结果如下:
在这里插入图片描述

6. 源码献上

github项目代码自取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值