springboot中添加静态页面的三种实现方案与对比

 更新时间:2025年11月05日 08:40:01   作者:悟能不能悟  
这篇文章主要为大家详细介绍了springboot中添加静态页面的三种实现方案与对比,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

在Spring Boot项目中添加HTML页面,我来为你详细介绍几种常见的方式:

方法1:使用Thymeleaf模板引擎(推荐)

1. 添加依赖

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

2. 创建HTML文件

src/main/resources/templates目录下创建 hello.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot HTML示例</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        .container { max-width: 800px; margin: 0 auto; }
        .header { background-color: #f0f0f0; padding: 20px; border-radius: 5px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 th:text="'Hello, ' + ${name} + '!'">Hello, World!</h1>
        </div>
        <p>当前时间: <span th:text="${currentTime}">2023-01-01</span></p>
        <button onclick="showAlert()">点击我</button>
    </div>

    <script>
        function showAlert() {
            alert('Hello from Spring Boot!');
        }
    </script>
</body>
</html>

3. 创建Controller

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

@Controller
public class HelloController {
    
    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("name", "Spring Boot");
        model.addAttribute("currentTime", LocalDateTime.now());
        return "hello"; // 对应templates/hello.html
    }
}

方法2:使用静态HTML(无需模板引擎)

1. 创建静态HTML

src/main/resources/static目录下创建 index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>静态页面示例</title>
</head>
<body>
    <h1>欢迎来到Spring Boot应用</h1>
    <p>这是一个静态HTML页面</p>
    <a href="/hello" rel="external nofollow" >访问动态页面</a>
</body>
</html>

2. 配置Controller(可选)

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

@Controller
public class IndexController {
    
    @GetMapping("/")
    public String index() {
        return "forward:/index.html"; // 重定向到静态页面
    }
}

方法3:使用FreeMarker模板

1. 添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2. 创建FreeMarker模板

src/main/resources/templates创建 welcome.ftl

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome, ${userName}!</h1>
    <p>Email: ${email}</p>
</body>
</html>

配置说明

应用配置(application.properties)

# Thymeleaf配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false  # 开发时关闭缓存

# 静态资源路径
spring.web.resources.static-locations=classpath:/static/

项目结构

src/main/
├── java/
│   └── com/example/demo/
│       └── DemoApplication.java
│       └── controller/
│           └── HelloController.java
└── resources/
    ├── static/
    │   └── index.html
    ├── templates/
    │   └── hello.html
    └── application.properties

启动应用

运行主应用类:

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

访问页面:

  • 静态页面:http://localhost:8080/index.html
  • 动态页面:http://localhost:8080/hello

注意事项

  • 模板位置​:Thymeleaf模板默认放在 src/main/resources/templates/
  • 静态资源​:CSS、JS、图片等放在 src/main/resources/static/
  • 热部署​:开发时可关闭模板缓存以便实时查看修改
  • 路径问题​:使用Thymeleaf时注意用 th:前缀代替原生HTML属性

选择哪种方式取决于你的需求:

  • 需要动态数据:使用Thymeleaf或FreeMarker
  • 纯静态内容:直接放在static目录
  • 简单页面:静态HTML足够
  • 复杂页面:推荐Thymeleaf

到此这篇关于springboot中添加静态页面的三种实现方案与对比的文章就介绍到这了,更多相关springboot添加静态页面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java截取字符串的几种常用方法

    Java截取字符串的几种常用方法

    这篇文章主要给大家介绍了关于Java截取字符串的几种常用方法,在Java编程语言中,String类提供了用于操作字符串的丰富方法,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • Spring Boot 集成 Quartz 使用Cron 表达式实现定时任务

    Spring Boot 集成 Quartz 使用Cron 表达式实现定

    本文介绍了如何在SpringBoot项目中集成Quartz并使用Cron表达式进行任务调度,通过添加Quartz依赖、创建Quartz任务、配置任务调度以及启动项目,可以实现定时任务的执行,Cron表达式提供了灵活的任务调度方式,适用于各种复杂的定时任务需求,感兴趣的朋友一起看看吧
    2025-03-03
  • 浅谈Java中SimpleDateFormat 多线程不安全原因

    浅谈Java中SimpleDateFormat 多线程不安全原因

    SimpleDateFormat是Java中用于日期时间格式化的一个类,本文主要介绍了浅谈Java中SimpleDateFormat 多线程不安全原因,感兴趣的可以了解一下
    2024-01-01
  • Java实现雪花算法(snowflake)

    Java实现雪花算法(snowflake)

    这篇文章主要介绍了Java实现雪花算法(snowflake),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • 解析Jmeter脱离Jenkins后Ant集成邮件通知问题

    解析Jmeter脱离Jenkins后Ant集成邮件通知问题

    今天来讲下本地的ant构建并发送邮件。配置下来挺顺利也挺简单的,对Jmeter脱离Jenkins后Ant集成邮件通知问题感兴趣的朋友跟随小编一起看看吧
    2021-12-12
  • java8中的HashMap原理详解

    java8中的HashMap原理详解

    这篇文章主要介绍了java8中的HashMap原理详解,HashMap是日常开发中非常常用的容器,HashMap实现了Map接口,底层的实现原理是哈希表,HashMap不是一个线程安全的容器,需要的朋友可以参考下
    2023-09-09
  • java集合PriorityQueue优先级队列方法实例

    java集合PriorityQueue优先级队列方法实例

    这篇文章主要为大家介绍了java集合PriorityQueue优先级队列方法实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java数据结构之线索化二叉树的实现

    Java数据结构之线索化二叉树的实现

    在二叉树的结点上加上线索的二叉树称为线索二叉树,对二叉树以某种遍历方式进行遍历,使其变为线索二叉树的过程称为对二叉树进行线索化。本文将详解如何实现线索化二叉树,需要的可以参考一下
    2022-05-05
  • Java编程之jdk1.4,jdk1.5和jdk1.6的区别分析(经典)

    Java编程之jdk1.4,jdk1.5和jdk1.6的区别分析(经典)

    这篇文章主要介绍了Java编程之jdk1.4,jdk1.5和jdk1.6的区别分析,结合实例形式较为详细的分析说明了jdk1.4,jdk1.5和jdk1.6版本的使用区别,需要的朋友可以参考下
    2015-12-12
  • SpringBoot2.0整合Shiro框架实现用户权限管理的示例

    SpringBoot2.0整合Shiro框架实现用户权限管理的示例

    这篇文章主要介绍了SpringBoot2.0整合Shiro框架实现用户权限管理的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08

最新评论