Spring?Boot整合Bootstrap的超詳細(xì)步驟
一、添加 Bootstrap 依賴
在 pom.xml 文件中添加以下依賴:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>這里使用 WebJars 來(lái)管理 Bootstrap 的依賴,它可以將前端框架作為一個(gè) jar 包進(jìn)行引用,方便管理和升級(jí)。
二、配置靜態(tài)資源
在 Spring Boot 項(xiàng)目中,靜態(tài)資源默認(rèn)放置在 src/main/resources/static 目錄下。因此,我們需要將 Bootstrap 的靜態(tài)資源也放置在該目錄下。
- 在 src/main/resources/static 目錄下新建一個(gè)名為webjars的目錄。
- 在 webjars 目錄下新建一個(gè)名為 bootstrap 的目錄。
- 將 bootstrap-5.1.3 目錄中的 css、js和 fonts 三個(gè)子目錄復(fù)制到src/main/resources/static/webjars/bootstrap 目錄下。
這樣,我們就成功將 Bootstrap 的靜態(tài)資源放置在了 Spring Boot 項(xiàng)目的靜態(tài)資源目錄下。
三、創(chuàng)建一個(gè) Bootstrap 頁(yè)面
<!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)行程序
訪問(wèn) http://localhost:8080,應(yīng)該能看到一個(gè)帶有標(biāo)題的頁(yè)面,這就說(shuō)明我們已經(jīng)成功地整合了 Bootstrap 前端框架。
五、使用 Bootstrap 組件
除了引入 Bootstrap 的樣式和腳本文件外,我們還可以使用 Bootstrap 提供的組件來(lái)構(gòu)建頁(yè)面。以下是一個(gè)使用 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>在這個(gè)頁(yè)面中,我們使用了 Bootstrap 的柵格系統(tǒng)來(lái)將表單組件布局為兩列,使用了表單組件來(lái)收集用戶的姓名和電子郵件地址,并使用了按鈕組件來(lái)提交表單。
高級(jí)用法:使用 Thymeleaf 和 Bootstrap
除了手動(dòng)編寫 HTML 頁(yè)面外,我們還可以使用 Thymeleaf 模板引擎來(lái)結(jié)合 Bootstrap 來(lái)構(gòu)建頁(yè)面。下面是一個(gè)使用 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>在這個(gè)頁(yè)面中,我們使用了 Thymeleaf 的表達(dá)式語(yǔ)言來(lái)動(dòng)態(tài)地生成表單組件,使用了 Thymeleaf 的表單綁定來(lái)將表單數(shù)據(jù)綁定到模型對(duì)象上。使用 Thymeleaf 可以讓我們更加便捷地生成 HTML 頁(yè)面,并且提供了強(qiáng)大的表達(dá)式語(yǔ)言來(lái)處理頁(yè)面邏輯。
使用CDN加速加載Bootstrap資源
在生產(chǎn)環(huán)境中,為了加速頁(yè)面加載速度,我們可以將 Bootstrap 的靜態(tài)資源文件放在 CDN 上。這樣可以減少服務(wù)器的壓力,并且可以利用 CDN 的分布式網(wǎng)絡(luò)加速頁(yè)面加載。以下是一個(gè)使用 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)建頁(yè)面時(shí),我們還可以使用 Thymeleaf Layouts 來(lái)更加方便地組織頁(yè)面結(jié)構(gòu)。Thymeleaf Layouts 是一款 Thymeleaf 模板引擎的擴(kuò)展,提供了布局和片段的功能,可以讓我們更加方便地重用頁(yè)面結(jié)構(gòu)。以下是一個(gè)使用 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>在這個(gè)頁(yè)面中,我們定義了一個(gè) layout 命名空間,并使用 layout 命名空間中的 title-pattern 屬性來(lái)動(dòng)態(tài)設(shè)置頁(yè)面標(biāo)題。在 header 片段中定義了導(dǎo)航欄,而 content 片段則留給子頁(yè)面來(lái)填充。
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>在子頁(yè)面中,我們使用 layout:decorate 屬性來(lái)引用 layout.html,并使用 header 和 content 片段來(lái)填充導(dǎo)航欄和主要內(nèi)容。在主要內(nèi)容中,我們使用 Bootstrap 的表單和網(wǎng)格系統(tǒng)來(lái)創(chuàng)建一個(gè)登錄表單和一個(gè)隨機(jī)圖片。
總結(jié)
到此這篇關(guān)于Spring Boot整合Bootstrap的超詳細(xì)步驟的文章就介紹到這了,更多相關(guān)SpringBoot整合Bootstrap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
knife4j?整合?springboot的過(guò)程詳解
這篇文章主要介紹了knife4j整合springboot的過(guò)程,本次整合springboot版本為2.3.12,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
centos下編譯安裝mysql報(bào)錯(cuò)解決方案
今天在centos6.2下面源碼編譯安裝mysql的時(shí),在編譯mysql的時(shí)候報(bào)了一個(gè)蛋蛋的錯(cuò)誤,本文提供詳細(xì)解決方案2012-11-11
SpringBoot整合阿里云開(kāi)通短信服務(wù)詳解
這篇文章主要介紹了如何利用SpringBoot整合阿里云實(shí)現(xiàn)短信服務(wù)的開(kāi)通,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-03-03
Java技巧分享之利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData
這篇文章主要來(lái)和大家分享一個(gè)Java技巧,那就是利用RxJava打造可觀測(cè)數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06
java使用歸并刪除法刪除二叉樹(shù)中節(jié)點(diǎn)的方法
這篇文章主要介紹了java使用歸并刪除法刪除二叉樹(shù)中節(jié)點(diǎn)的方法,實(shí)例分析了java二叉樹(shù)算法的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
關(guān)于SpringMVC在Controller層方法的參數(shù)解析詳解
在SpringMVC中,控制器Controller負(fù)責(zé)處理由DispatcherServlet分發(fā)的請(qǐng)求,下面這篇文章主要給大家介紹了關(guān)于SpringMVC在Controller層方法的參數(shù)解析的相關(guān)資料,需要的朋友可以參考下2021-12-12
JavaSE static final及abstract修飾符實(shí)例解析
這篇文章主要介紹了JavaSE static final及abstract修飾符實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

