位置: IT常识 - 正文

Spring Boot 整合 Bootstrap(spring boot整合hibernate)

编辑:rootadmin
Spring Boot 整合 Bootstrap Spring Boot 整合 Bootstrap一、添加 Bootstrap 依赖二、配置静态资源三、创建一个 Bootstrap 页面运行程序五、使用 Bootstrap 组件高级用法:使用 Thymeleaf 和 Bootstrap使用CDN加速加载Bootstrap资源使用Thymeleaf Layoutsindex.html一、添加 Bootstrap 依赖

推荐整理分享Spring Boot 整合 Bootstrap(spring boot整合hibernate),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:spring boot整合aop,springboot整合bootstrap,spring boot整合ssm,spring boot整合aop,Spring boot 整合项目,spring boot整合ssm,springboot整合bootstrap,Spring boot 整合项目 清华大学,内容如对您有帮助,希望把文章链接给更多的朋友!

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

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

这里使用 WebJars 来管理 Bootstrap 的依赖,它可以将前端框架作为一个 jar 包进行引用,方便管理和升级。

二、配置静态资源

在 Spring Boot 项目中,静态资源默认放置在 src/main/resources/static 目录下。因此,我们需要将 Bootstrap 的静态资源也放置在该目录下。

在 src/main/resources/static 目录下新建一个名为webjars的目录。在 webjars 目录下新建一个名为 bootstrap 的目录。将 bootstrap-5.1.3 目录中的 css、js和 fonts 三个子目录复制到src/main/resources/static/webjars/bootstrap 目录下。

这样,我们就成功将 Bootstrap 的静态资源放置在了 Spring Boot 项目的静态资源目录下。

三、创建一个 Bootstrap 页面<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Spring Boot + Bootstrap</title> <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"></head><body> <div class="container"> <h1>Hello, Spring Boot!</h1> </div> <script src="/webjars/bootstrap/js/bootstrap.min.js"></script></body></html>运行程序Spring Boot 整合 Bootstrap(spring boot整合hibernate)

访问 http://localhost:8080,应该能看到一个带有标题的页面,这就说明我们已经成功地整合了 Bootstrap 前端框架。

五、使用 Bootstrap 组件

除了引入 Bootstrap 的样式和脚本文件外,我们还可以使用 Bootstrap 提供的组件来构建页面。以下是一个使用 Bootstrap 栅格系统和表单组件的示例:

<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Spring Boot + Bootstrap</title> <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css"></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 的栅格系统来将表单组件布局为两列,使用了表单组件来收集用户的姓名和电子邮件地址,并使用了按钮组件来提交表单。

高级用法:使用 Thymeleaf 和 Bootstrap

除了手动编写 HTML 页面外,我们还可以使用 Thymeleaf 模板引擎来结合 Bootstrap 来构建页面。下面是一个使用 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"></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 的表达式语言来动态地生成表单组件,使用了 Thymeleaf 的表单绑定来将表单数据绑定到模型对象上。使用 Thymeleaf 可以让我们更加便捷地生成 HTML 页面,并且提供了强大的表达式语言来处理页面逻辑。

使用CDN加速加载Bootstrap资源

在生产环境中,为了加速页面加载速度,我们可以将 Bootstrap 的静态资源文件放在 CDN 上。这样可以减少服务器的压力,并且可以利用 CDN 的分布式网络加速页面加载。以下是一个使用 CDN 加速加载 Bootstrap 资源的示例:

<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Spring Boot + Bootstrap</title> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.2/css/bootstrap.min.css"></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 构建页面时,我们还可以使用 Thymeleaf Layouts 来更加方便地组织页面结构。Thymeleaf Layouts 是一款 Thymeleaf 模板引擎的扩展,提供了布局和片段的功能,可以让我们更加方便地重用页面结构。以下是一个使用 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}"></head><body> <header layout:fragment="header"> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">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="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Features</a> </li> <li class="nav-item"> <a class="nav-link" href="#">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 属性来动态设置页面标题。在 header 片段中定义了导航栏,而 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 片段来填充导航栏和主要内容。在主要内容中,我们使用 Bootstrap 的表单和网格系统来创建一个登录表单和一个随机图片。

本文链接地址:https://www.jiuchutong.com/zhishi/298775.html 转载请保留说明!

上一篇:JavaScript-百炼成仙(第1节掌握JavaScript基础1.1-1.21)(javascript百炼成仙免费)

下一篇:【第二趴】uni-app开发工具(手把手带你安装HBuilderX、搭建第一个多端项目初体验)

  • 苹果13promax是刘海屏吗(iphone13promax是5g手机吗)

    苹果13promax是刘海屏吗(iphone13promax是5g手机吗)

  • vivo手机怎么进入工程模式(vivo手机怎么进入隐私空间)

    vivo手机怎么进入工程模式(vivo手机怎么进入隐私空间)

  • 京东怎么修改定位(京东怎么修改定位领取北京优惠券)

    京东怎么修改定位(京东怎么修改定位领取北京优惠券)

  • mtkg85相当于骁龙多少(mtkg85处理器)

    mtkg85相当于骁龙多少(mtkg85处理器)

  • p30pro夜视仪功能没了(华为p30pro夜视功能怎么用)

    p30pro夜视仪功能没了(华为p30pro夜视功能怎么用)

  • iphone7录屏没有声音(为什么录屏没有声音苹果7)

    iphone7录屏没有声音(为什么录屏没有声音苹果7)

  • iqoo算什么档次的手机(iqoo是大品牌吗)

    iqoo算什么档次的手机(iqoo是大品牌吗)

  • 抖音首发是什么意思(抖音短视频首发啥意思)

    抖音首发是什么意思(抖音短视频首发啥意思)

  • 键盘怎么打出小写字母(键盘怎么打出小数字)

    键盘怎么打出小写字母(键盘怎么打出小数字)

  • 苹果x13.2版本来闪光灯打开了怎么不闪(苹果x13.2.3系统怎么样)

    苹果x13.2版本来闪光灯打开了怎么不闪(苹果x13.2.3系统怎么样)

  • 蜂窝移动网络显示错误怎么设置(蜂窝移动网络显示错误)

    蜂窝移动网络显示错误怎么设置(蜂窝移动网络显示错误)

  • 苹果手机自动打开wifi怎么回事(苹果手机自动打电话出去怎么解决)

    苹果手机自动打开wifi怎么回事(苹果手机自动打电话出去怎么解决)

  • 免费安全共享wif能连吗(免费安全共享)

    免费安全共享wif能连吗(免费安全共享)

  • 删去的照片怎么免费恢复(删去的照片怎么样才能找回来)

    删去的照片怎么免费恢复(删去的照片怎么样才能找回来)

  • 苹果相册可以加密码吗(苹果相册可以加贴纸吗)

    苹果相册可以加密码吗(苹果相册可以加贴纸吗)

  • oppo换机助手在哪(oppo手机的换机助手怎么换出)

    oppo换机助手在哪(oppo手机的换机助手怎么换出)

  • p30pro红外传感器有什么用(华为p30 pro红外传感器是干什么的)

    p30pro红外传感器有什么用(华为p30 pro红外传感器是干什么的)

  • 华为保时捷mate30什么时候上市

    华为保时捷mate30什么时候上市

  • 闲鱼拍卖买家可以取消吗(闲鱼拍卖买家可以自己取消吗)

    闲鱼拍卖买家可以取消吗(闲鱼拍卖买家可以自己取消吗)

  • qq怎么改主题自定义免费(qq主题自己设的怎么改)

    qq怎么改主题自定义免费(qq主题自己设的怎么改)

  • 咸鱼退款卖家不处理怎么办(咸鱼退款卖家不同意可以退款吗?)

    咸鱼退款卖家不处理怎么办(咸鱼退款卖家不同意可以退款吗?)

  • 手机云闪付功能在哪里(云闪付的手机闪付功能)

    手机云闪付功能在哪里(云闪付的手机闪付功能)

  • 淘宝付定金立减是什么意思(淘宝付定金立减在哪里看)

    淘宝付定金立减是什么意思(淘宝付定金立减在哪里看)

  • 苹果怎么下载不了喜马拉雅(苹果怎么下载不需要密码)

    苹果怎么下载不了喜马拉雅(苹果怎么下载不需要密码)

  • 苹果6p电池容量(苹果14的电池容量)

    苹果6p电池容量(苹果14的电池容量)

  • 手机液晶屏漏液修复(手机液晶屏漏液怎么防止扩大)

    手机液晶屏漏液修复(手机液晶屏漏液怎么防止扩大)

  • 税负转嫁的概念是什么
  • 电子税务局实名核验失败怎么回事啊
  • 个体工商户营业执照年检网上申报
  • 个人所得税汇算清缴情况报告
  • 企业开办费可不交税吗
  • 工伤保险应该计入什么科目
  • 个体注销了名下的车辆
  • 外币结汇怎么做账
  • 平均房租
  • 领用包装物会计处理
  • 帮别的公司做账的叫什么公司
  • 建筑工程属于开票项目吗
  • 技术服务费属于无形资产吗
  • 发票最大限额999元,超过了怎么办
  • 6%的增值税发票能抵扣13%的吗
  • 股权变更之后税务变更
  • 付工程款现金怎么做凭证?
  • 持续经营净利润和扣非净利润
  • 四项服务加计扣除政策2023
  • 已抵扣的增值税专票如何冲红
  • 材料成本的核算方法有哪些
  • 货币资金包括哪些
  • 销售环节的运费怎么算
  • 补提去年所得税分录
  • 航天信息服务费是什么费用
  • bios设置内存频率后黑屏
  • window10自带商店下载位置
  • kcleaner.exe是什么
  • linux系统已经得到了广泛的应用
  • 2021新旧会计准则
  • Linux系统中sort排序命令的使用教程
  • 工程施工广告牌
  • wifi增强器1200m的是不是比较好
  • PHP:mcrypt_decrypt()的用法_Mcrypt函数
  • 企业发生的诉讼费用
  • 圣何塞在哪
  • 以前年度进项转出分录
  • 微信小程序设计规范(官方)文档
  • 前端经典面试题及答案
  • 免税票如何做会计分录
  • 消费税算不算运费
  • 月初领票是不是要等到报完税才可以领
  • mysql 索引 key
  • 金税四期来了我们前期要准备什么
  • 对公账户 退款
  • 企业支付的佣金计算多少税率呢
  • 行政事业单位过节费发放规定
  • 利润表中资产减值损失为正数是什么原因
  • 银行保函会计处理
  • 结转未交增值税为什么是零
  • 老板想提取销售怎么办
  • 发票验旧后才能领新发票吗
  • 对公网银回单可以导出吗
  • 原始单据的重要性
  • 安装sqlserver2000 IP地址出错怎样修改
  • Navicat for MySQL(mysql图形化管理工具)是什么?
  • sqlserver bulkcopy
  • 如何提高windows7运行速度
  • flashplayer不能正常运行
  • linux下nanosleep() & sleep()的区别
  • wps文字打印不清楚怎么办
  • linux更新内核的好处
  • windos8怎么样
  • win10缺少文件怎么办
  • win8适用的pr
  • win7如何清理c盘空间不影响系统
  • git打标签命令
  • cocos roadmap
  • Coroutine couldn't be started because the the game object 'GameController' is inactive!
  • android 开发环境
  • 第一章初见第二章决定
  • vuejs单页面
  • vue+vue-validator 表单验证功能的实现代码
  • python文件管理系统难点总结
  • jquery validator
  • 基础的重要性
  • 解决烧心最快方法
  • 在北京税前工资8000算多的吗
  • 教育培训行业的发展前景
  • 国外工资个人所得税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设