欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot整合Bootstrap的超詳細(xì)步驟

 更新時間:2023年05月08日 11:06:07   作者:wangkay88  
之前做前端開發(fā),在使用bootstrap的時候都是去官網(wǎng)下載,然后放到項(xiàng)目中,在頁面引用,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot整合Bootstrap的超詳細(xì)步驟,需要的朋友可以參考下

一、添加 Bootstrap 依賴

在 pom.xml 文件中添加以下依賴:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>5.1.3</version>
</dependency>

這里使用 WebJars 來管理 Bootstrap 的依賴,它可以將前端框架作為一個 jar 包進(jìn)行引用,方便管理和升級。

二、配置靜態(tài)資源

在 Spring Boot 項(xiàng)目中,靜態(tài)資源默認(rèn)放置在 src/main/resources/static 目錄下。因此,我們需要將 Bootstrap 的靜態(tài)資源也放置在該目錄下。

  • 在 src/main/resources/static 目錄下新建一個名為webjars的目錄。
  • 在 webjars 目錄下新建一個名為 bootstrap 的目錄。
  • 將 bootstrap-5.1.3 目錄中的 css、js和 fonts 三個子目錄復(fù)制到src/main/resources/static/webjars/bootstrap 目錄下。

這樣,我們就成功將 Bootstrap 的靜態(tài)資源放置在了 Spring Boot 項(xiàng)目的靜態(tài)資源目錄下。

三、創(chuàng)建一個 Bootstrap 頁面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Spring Boot + Bootstrap</title>
    <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
</head>
<body>
    <div class="container">
        <h1>Hello, Spring Boot!</h1>
    </div>
    <script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

運(yùn)行程序

訪問 http://localhost:8080,應(yīng)該能看到一個帶有標(biāo)題的頁面,這就說明我們已經(jīng)成功地整合了 Bootstrap 前端框架。

五、使用 Bootstrap 組件

除了引入 Bootstrap 的樣式和腳本文件外,我們還可以使用 Bootstrap 提供的組件來構(gòu)建頁面。以下是一個使用 Bootstrap 柵格系統(tǒng)和表單組件的示例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Spring Boot + Bootstrap</title>
    <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
</head>
<body>
    <div class="container">
        <h1>Hello, Spring Boot!</h1>
        <div class="row">
            <div class="col-md-6">
                <form>
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name">
                    </div>
                    <div class="form-group">
                        <label for="inputEmail">Email</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email">
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
    <script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

在這個頁面中,我們使用了 Bootstrap 的柵格系統(tǒng)來將表單組件布局為兩列,使用了表單組件來收集用戶的姓名和電子郵件地址,并使用了按鈕組件來提交表單。

高級用法:使用 Thymeleaf 和 Bootstrap

除了手動編寫 HTML 頁面外,我們還可以使用 Thymeleaf 模板引擎來結(jié)合 Bootstrap 來構(gòu)建頁面。下面是一個使用 Thymeleaf 和 Bootstrap 的示例:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot + Bootstrap + Thymeleaf</title>
    <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
</head>
<body>
    <div class="container">
        <h1>Hello, Spring Boot!</h1>
        <div class="row">
            <div class="col-md-6">
                <form th:action="@{/submit}" method="post">
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name" th:field="*{name}">
                    </div>
                    <div class="form-group">
                        <label for="inputEmail">Email</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email" th:field="*{email}">
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
    <script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

在這個頁面中,我們使用了 Thymeleaf 的表達(dá)式語言來動態(tài)地生成表單組件,使用了 Thymeleaf 的表單綁定來將表單數(shù)據(jù)綁定到模型對象上。使用 Thymeleaf 可以讓我們更加便捷地生成 HTML 頁面,并且提供了強(qiáng)大的表達(dá)式語言來處理頁面邏輯。

使用CDN加速加載Bootstrap資源

在生產(chǎn)環(huán)境中,為了加速頁面加載速度,我們可以將 Bootstrap 的靜態(tài)資源文件放在 CDN 上。這樣可以減少服務(wù)器的壓力,并且可以利用 CDN 的分布式網(wǎng)絡(luò)加速頁面加載。以下是一個使用 CDN 加速加載 Bootstrap 資源的示例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Spring Boot + Bootstrap</title>
    <link rel="stylesheet"  rel="external nofollow" >
</head>
<body>
    <div class="container">
        <h1>Hello, Spring Boot!</h1>
        <div class="row">
            <div class="col-md-6">
                <form>
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name">
                    </div>
                    <div class="form-group">
                        <label for="inputEmail">Email</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email">
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.2/js/bootstrap.min.js"></script>
</body>
</html>

使用Thymeleaf Layouts

在使用 Thymeleaf 和 Bootstrap 構(gòu)建頁面時,我們還可以使用 Thymeleaf Layouts 來更加方便地組織頁面結(jié)構(gòu)。Thymeleaf Layouts 是一款 Thymeleaf 模板引擎的擴(kuò)展,提供了布局和片段的功能,可以讓我們更加方便地重用頁面結(jié)構(gòu)。以下是一個使用 Thymeleaf Layouts 的示例:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8">
    <title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Spring Boot + Bootstrap</title>
    <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" rel="external nofollow" >
</head>
<body>
    <header layout:fragment="header">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >Navbar</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >Features</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >Pricing</a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>
    <div class="container">
        <section layout:fragment="content"></section>
    </div>
    <script th:src="@{/webjars/jquery/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
</body>
</html>

在這個頁面中,我們定義了一個 layout 命名空間,并使用 layout 命名空間中的 title-pattern 屬性來動態(tài)設(shè)置頁面標(biāo)題。在 header 片段中定義了導(dǎo)航欄,而 content 片段則留給子頁面來填充。

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="layout">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
    <header layout:fragment="header"></header>
    <section layout:fragment="content">
        <h1>Hello, Spring Boot!</h1>
        <div class="row">
            <div class="col-md-6">
                <form>
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name">
                            <div class="form-group">
                    <label for="inputEmail">Email address</label>
                    <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email">
                </div>
                <div class="form-group">
                    <label for="inputPassword">Password</label>
                    <input type="password" class="form-control" id="inputPassword" placeholder="Enter your password">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
        <div class="col-md-6">
            <img src="https://picsum.photos/500/300" alt="Random image" class="img-fluid">
        </div>
    </div>
</section>
</body>
</html>

在子頁面中,我們使用 layout:decorate 屬性來引用 layout.html,并使用 header 和 content 片段來填充導(dǎo)航欄和主要內(nèi)容。在主要內(nèi)容中,我們使用 Bootstrap 的表單和網(wǎng)格系統(tǒng)來創(chuàng)建一個登錄表單和一個隨機(jī)圖片。

總結(jié)

到此這篇關(guān)于Spring Boot整合Bootstrap的超詳細(xì)步驟的文章就介紹到這了,更多相關(guān)SpringBoot整合Bootstrap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java解決程序包不存在的問題解決

    Java解決程序包不存在的問題解決

    在Java編程中,我們可以使用Maven或Gradle等構(gòu)建工具來管理依賴庫,本文主要介紹了Java解決程序包不存在的問題解決,具有一定的參考價值,感興趣的可以了解 一下
    2023-12-12
  • Java實(shí)現(xiàn)分頁代碼

    Java實(shí)現(xiàn)分頁代碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)分頁代碼,提高查詢效率,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • knife4j?整合?springboot的過程詳解

    knife4j?整合?springboot的過程詳解

    這篇文章主要介紹了knife4j整合springboot的過程,本次整合springboot版本為2.3.12,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • centos下編譯安裝mysql報(bào)錯解決方案

    centos下編譯安裝mysql報(bào)錯解決方案

    今天在centos6.2下面源碼編譯安裝mysql的時,在編譯mysql的時候報(bào)了一個蛋蛋的錯誤,本文提供詳細(xì)解決方案
    2012-11-11
  • SpringBoot整合阿里云開通短信服務(wù)詳解

    SpringBoot整合阿里云開通短信服務(wù)詳解

    這篇文章主要介紹了如何利用SpringBoot整合阿里云實(shí)現(xiàn)短信服務(wù)的開通,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定幫助,需要的可以參考一下
    2022-03-03
  • Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData

    Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData

    這篇文章主要來和大家分享一個Java技巧,那就是利用RxJava打造可觀測數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-06-06
  • java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法

    java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法

    這篇文章主要介紹了java使用歸并刪除法刪除二叉樹中節(jié)點(diǎn)的方法,實(shí)例分析了java二叉樹算法的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • 關(guān)于SpringMVC在Controller層方法的參數(shù)解析詳解

    關(guān)于SpringMVC在Controller層方法的參數(shù)解析詳解

    在SpringMVC中,控制器Controller負(fù)責(zé)處理由DispatcherServlet分發(fā)的請求,下面這篇文章主要給大家介紹了關(guān)于SpringMVC在Controller層方法的參數(shù)解析的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • JavaSE static final及abstract修飾符實(shí)例解析

    JavaSE static final及abstract修飾符實(shí)例解析

    這篇文章主要介紹了JavaSE static final及abstract修飾符實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • JAVA中簡單的for循環(huán)異常踩坑

    JAVA中簡單的for循環(huán)異常踩坑

    這篇文章主要為大家介紹了JAVA中簡單的for循環(huán)異常踩坑避雷詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07

最新評論