当前位置: 首页 > news >正文

如何在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">访问动态页面</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

启动应用

  1. 运行主应用类:

@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
  1. 访问页面:

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

    • 动态页面:http://localhost:8080/hello

注意事项

  1. 模板位置​:Thymeleaf模板默认放在 src/main/resources/templates/

  2. 静态资源​:CSS、JS、图片等放在 src/main/resources/static/

  3. 热部署​:开发时可关闭模板缓存以便实时查看修改

  4. 路径问题​:使用Thymeleaf时注意用 th:前缀代替原生HTML属性

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

  • 需要动态数据:使用Thymeleaf或FreeMarker

  • 纯静态内容:直接放在static目录

  • 简单页面:静态HTML足够

  • 复杂页面:推荐Thymeleaf

http://www.dtcms.com/a/570075.html

相关文章:

  • 北京网站设计外包公司大兴高米店网站建设
  • Linux 进程通信(IPC)一站式笔记:概念 → 常用方式 → 函数原型与参数详解
  • 盐城网站推广wordpress手机pc分开模板
  • 建立网站时要采用一定的链接结构网站建设最简单的教程视频教程
  • 泰安手机网站建设电话肇庆自助网站建设系统
  • 基于Vue的地铁综合服务管理系统7949eg04(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 房产网站建设接单如何在手机上做微电影网站
  • Oracle AWR管理与快照操作完整指南
  • 济源专业做网站公司又拍云WordPress 插件
  • 蓝色风格网站网站案例上海
  • 如何在 MySQL Server 中配置 SSL 证书 ?
  • UGUI笔记——3D坐标转换成UGUI坐标
  • 2025 | 时序预测新范式:多智能体AI系统实现全流程自动化!
  • 营销型网站建设模板郴州网络科技有限公司
  • 网站服务器如何做热备价网络公司排名前十名有哪些
  • 视频转图片工具
  • 长春网站建设方案优化网络规划设计师教程第二版电子版
  • 怎样建商业网站wordpress升级机制
  • CANN算子开发实战:从动态Shape到测试验证的深度解析
  • re一下--day8--字符串(一)
  • 网站关键词在哪里修改网站建设80hoe
  • 企业数据服务新选择:“五度易链” SaaS/API/ 本地化部署方案适配全规模需求
  • 【JUnit实战3_27】第十六章:用 JUnit 测试 Spring 应用:通过实战案例深入理解 IoC 原理
  • 网站源码模板免费网站服务器2020
  • ftp怎么连接网站空间如何建立外贸网站
  • 不同防滑设计在复杂牙拔除中的效能评估
  • 基于springboot的精准扶贫管理系统开发与设计
  • 电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2025年9月)
  • linux系统rsync文件传输
  • 服务器建站用哪个系统好新闻稿件