最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比

 更新時間:2025年11月05日 08:40:01   作者:悟能不能悟  
這篇文章主要為大家詳細介紹了springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

在Spring Boot項目中添加HTML頁面,我來為你詳細介紹幾種常見的方式:

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

1. 添加依賴

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

2. 創(chuàng)建HTML文件

src/main/resources/templates目錄下創(chuàng)建 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. 創(chuàng)建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:使用靜態(tài)HTML(無需模板引擎)

1. 創(chuàng)建靜態(tài)HTML

src/main/resources/static目錄下創(chuàng)建 index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>靜態(tài)頁面示例</title>
</head>
<body>
    <h1>歡迎來到Spring Boot應用</h1>
    <p>這是一個靜態(tài)HTML頁面</p>
    <a href="/hello" rel="external nofollow" >訪問動態(tài)頁面</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"; // 重定向到靜態(tài)頁面
    }
}

方法3:使用FreeMarker模板

1. 添加依賴

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

2. 創(chuàng)建FreeMarker模板

src/main/resources/templates創(chuàng)建 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  # 開發(fā)時關閉緩存

# 靜態(tài)資源路徑
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);
    }
}

訪問頁面:

  • 靜態(tài)頁面:http://localhost:8080/index.html
  • 動態(tài)頁面:http://localhost:8080/hello

注意事項

  • ?模板位置?:Thymeleaf模板默認放在 src/main/resources/templates/
  • ?靜態(tài)資源?:CSS、JS、圖片等放在 src/main/resources/static/
  • ?熱部署?:開發(fā)時可關閉模板緩存以便實時查看修改
  • ?路徑問題?:使用Thymeleaf時注意用 th:前綴代替原生HTML屬性

選擇哪種方式取決于你的需求:

  • 需要動態(tài)數據:使用Thymeleaf或FreeMarker
  • 純靜態(tài)內容:直接放在static目錄
  • 簡單頁面:靜態(tài)HTML足夠
  • 復雜頁面:推薦Thymeleaf

到此這篇關于springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比的文章就介紹到這了,更多相關springboot添加靜態(tài)頁面內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot整合Hibernate Validator實現(xiàn)參數驗證功能

    SpringBoot整合Hibernate Validator實現(xiàn)參數驗證功能

    這篇文章主要介紹了SpringBoot整合Hibernate Validator實現(xiàn)參數驗證功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • 基于FileNotFoundException問題的解決

    基于FileNotFoundException問題的解決

    這篇文章主要介紹了基于FileNotFoundException問題的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java 泛型通配符 <? extends> vs <? super> 實戰(zhàn)場景

    Java 泛型通配符 <? extends> vs <? s

    本文解析Java泛型通配符<? extends T>和<? super T>的核心區(qū)別與應用場景,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-12-12
  • java List去掉重復元素的幾種方式(小結)

    java List去掉重復元素的幾種方式(小結)

    這篇文章主要介紹了java List去掉重復元素的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • RocketMQ重試機制及消息冪代碼實例解析

    RocketMQ重試機制及消息冪代碼實例解析

    這篇文章主要介紹了RocketMQ重試機制及消息冪代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java中的集合框架

    Java中的集合框架

    本文主要介紹了Java中集合框架的相關知識,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • java實現(xiàn)驗證碼類生成中文驗證碼

    java實現(xiàn)驗證碼類生成中文驗證碼

    java實現(xiàn)的漢字輸入驗證碼,主要包含兩個類,一個是生成驗證碼,一個是判斷驗證碼輸入是否正確,實現(xiàn)原理非常簡單,將漢字和干擾線生成圖片并將漢字保存到session,前臺獲取每次生成驗證碼圖片并用文本框值和session值比較,功能就怎么簡單
    2014-01-01
  • java學生信息管理系統(tǒng)源代碼

    java學生信息管理系統(tǒng)源代碼

    這篇文章主要為大家詳細介紹了java學生信息管理系統(tǒng)源代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Spring Boot系列教程之死信隊列詳解

    Spring Boot系列教程之死信隊列詳解

    這篇文章主要給大家介紹了關于Spring Boot系列教程之死信隊列的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • Java輕松實現(xiàn)表單提交的三種方法

    Java輕松實現(xiàn)表單提交的三種方法

    在Web開發(fā)中,表單是用戶與網站交互的主要方式之一,本文將詳細介紹如何在Java中實現(xiàn)表單提交,并通過代碼和案例為新手朋友提供詳細的指導,有需要的可以參考下
    2024-10-10

最新評論

昭平县| 扎赉特旗| 霍邱县| 英吉沙县| 瑞昌市| 资源县| 阿克苏市| 新宁县| 湾仔区| 鹿泉市| 贵溪市| 鹰潭市| 绥棱县| 灵璧县| 永年县| 永修县| 兴安盟| 比如县| 甘泉县| 河北省| 秦皇岛市| 柳林县| 镇雄县| 鹿泉市| 手游| 香格里拉县| 云南省| 普洱| 常德市| 巴彦淖尔市| 濉溪县| 临朐县| 镇原县| 安阳市| 贵州省| 七台河市| 延安市| 铜陵市| 当阳市| 五莲县| 克拉玛依市|