【Spring Boot】Spring Boot + Thymeleaf搭建mvc项目

Spring Boot + Thymeleaf搭建mvc项目

      • [1. 创建Spring Boot项目](#1. 创建Spring Boot项目)
      • [2. 配置pom.xml](#2. 配置pom.xml)
      • [3. 配置Thymeleaf](#3. 配置Thymeleaf)
      • [4. 创建Controller](#4. 创建Controller)
      • [5. 创建Thymeleaf页面](#5. 创建Thymeleaf页面)
      • [6. 创建Main启动类](#6. 创建Main启动类)
      • [7. 运行项目](#7. 运行项目)
      • [8. 测试结果](#8. 测试结果)
      • 扩展:添加静态资源

1. 创建Spring Boot项目

  1. 打开IntelliJ IDEA → New Project → 选择Maven → 直接点击Create
  2. 输入项目名称:springboot-demo
    确认GroupId(如:vip.buddha)和ArtifactId(保持与项目名一致)

2. 配置pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version> <!-- 选择一个 2.x 版本 -->
    </parent>

    <groupId>vip.buddha</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

</project>

依赖中无需指定版本的条件

  • 当依赖项属于 Spring Boot 官方提供的 Starter(如 spring-boot-starter-*),且父 POM 已通过<dependencyManagement> 管理其版本时。

需要指定版本的条件

  • 使用非 Spring Boot 官方维护的第三方库(如 com.google.guava),此时必须显式指定版本。

3. 配置Thymeleaf

Spring Boot默认已配置Thymeleaf,但你可以自定义设置。在 src/main/resources/application.yml 中添加:

yml 复制代码
server:
  port: 8080
spring:
  application:
    name: springboot-demo
  thymeleaf:
  	# 开发时建议开启缓存
    cache: false
    # 设置编码
    encoding: utf-8

4. 创建Controller

src/main/java/vip/buddha/controller 下新建一个Java类 HelloController.java

java 复制代码
package vip.buddha.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String sayHello(Model model) {
        model.addAttribute("message", "Hello, Thymeleaf!");
        return "hello"; // 对应src/main/resources/templates/hello.html
    }
}

5. 创建Thymeleaf页面

src/main/resources/templates 下新建 hello.html

html 复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot + Thymeleaf</title>
</head>
<body>
    <h1 th:text="${message}">默认消息</h1>
</body>
</html>
  • th:text="${message}" 会替换为Controller中设置的 message 值。

6. 创建Main启动类

src/main/java/vip/buddha 下新建一个Java类 Main.java

java 复制代码
package vip.buddha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

7. 运行项目

创建完毕目录结构如下:

  1. 找到主类 Main.java,右键运行 main 方法。
  2. 控制台输出 Started Main 表示启动成功。
  3. 访问 http://localhost:8080/hello 查看页面。

8. 测试结果

页面会显示:

复制代码
Hello, Thymeleaf!

Spring Boot 的启动类(Main Class)是整个应用程序的入口点,负责初始化 Spring 上下文并启动应用。

@SpringBootApplication组合了以下三个注解的功能:

  • @SpringBootConfiguration(标记当前类为 Spring Boot

    的配置类);

  • @EnableAutoConfiguration(启用 Spring Boot 的自动配置机制);

  • @ComponentScan(自动扫描当前包及其子包下的组件(如 @Controller, @Service, @Repository

    等))


扩展:添加静态资源

  1. src/main/resources/static 下放置CSS/JS文件(如 style.css)。
  2. 在HTML中引用:
html 复制代码
<link th:href="@{/style.css}" rel="stylesheet">

相关推荐
想用offer打牌1 天前
RocketMQ如何防止消息丢失?
java·后端·架构·开源·rocketmq
皮卡龙1 天前
Java常用的JSON
java·开发语言·spring boot·json
源码获取_wx:Fegn08951 天前
基于springboot + vue健身房管理系统
java·开发语言·前端·vue.js·spring boot·后端·spring
JIngJaneIL1 天前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
码事漫谈1 天前
Vibe Coding时代:人人都是开发者
后端
2501_916766541 天前
【Spring框架】SpringJDBC
java·后端·spring
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue图书管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
AntBlack1 天前
忍不住推荐 : AI 时代 ,桌面端真的可以考虑一下Go+Wails 的组合
后端·go·ai编程
码事漫谈1 天前
C++20协程如何撕开异步编程的牢笼
后端
DevYK1 天前
Coze Studio 二次开发(二)支持 MCP Server 动态配置
后端·agent·coze