位置: 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、搭建第一个多端项目初体验)

  • 浅谈人才网站网络营销渠道推广(人才网站是什么)

    浅谈人才网站网络营销渠道推广(人才网站是什么)

  • 荣耀50se微信视频美颜怎么开(荣耀50se微信视频没声音)

    荣耀50se微信视频美颜怎么开(荣耀50se微信视频没声音)

  • n卡是什么(a卡和n卡是什么)

    n卡是什么(a卡和n卡是什么)

  • WPS文本类型文件怎么设置(wps文本类型文件是什么)

    WPS文本类型文件怎么设置(wps文本类型文件是什么)

  • 清晰度在哪里设置(清晰度在哪里设置比较好)

    清晰度在哪里设置(清晰度在哪里设置比较好)

  • 路由器买450m和1200m好(450m路由器和1200m路由器哪个好)

    路由器买450m和1200m好(450m路由器和1200m路由器哪个好)

  • 快手什么时间段发作品播放量比较高(快手什么时间段发作品好上热门)

    快手什么时间段发作品播放量比较高(快手什么时间段发作品好上热门)

  • 启用宏的word文档是什么意思(启用宏的word文档是doc还是docx)

    启用宏的word文档是什么意思(启用宏的word文档是doc还是docx)

  • 淘宝怎么隐藏会员名(淘宝怎么隐藏会员名称)

    淘宝怎么隐藏会员名(淘宝怎么隐藏会员名称)

  • 京东淘客平台叫什么(淘客联盟京东怎么返利)

    京东淘客平台叫什么(淘客联盟京东怎么返利)

  • 群主误退群了怎么办(群主误退群怎么解散群)

    群主误退群了怎么办(群主误退群怎么解散群)

  • beats耳机怎么开机(beats耳机怎么开启降噪)

    beats耳机怎么开机(beats耳机怎么开启降噪)

  • 抖音的共同好友指什么(抖音的共同好友在哪里看)

    抖音的共同好友指什么(抖音的共同好友在哪里看)

  • 咪咕视频15元怎么取消(咪咕视频怎么收费的)

    咪咕视频15元怎么取消(咪咕视频怎么收费的)

  • 淘宝店铺会员收费吗(淘宝店铺会员收多少钱)

    淘宝店铺会员收费吗(淘宝店铺会员收多少钱)

  • 什么叫ods(什么叫odm)

    什么叫ods(什么叫odm)

  • 怎么知道对方qq注销了(怎么知道对方qq是否在线)

    怎么知道对方qq注销了(怎么知道对方qq是否在线)

  • 微信收不了钱怎么办(微信收不了钱怎么回事,显示账户异常)

    微信收不了钱怎么办(微信收不了钱怎么回事,显示账户异常)

  • 苹果11pro电池百分比怎么设置(苹果11pro电池百分比)

    苹果11pro电池百分比怎么设置(苹果11pro电池百分比)

  • 亚马逊美国站开店规则 (亚马逊美国站开店费用多少钱)

    亚马逊美国站开店规则 (亚马逊美国站开店费用多少钱)

  • 联通卡为什么发不了短信(联通卡为什么发送不了)

    联通卡为什么发不了短信(联通卡为什么发送不了)

  • oppok3截图在哪里(oppok3截屏)

    oppok3截图在哪里(oppok3截屏)

  • 百度知道如何用模板答题(用百度知道怎么赚现金)

    百度知道如何用模板答题(用百度知道怎么赚现金)

  • mem patch什么意思(mechanisms是什么意思)

    mem patch什么意思(mechanisms是什么意思)

  • 淘手游实名认证要多久(淘手游实名认证有风险吗)

    淘手游实名认证要多久(淘手游实名认证有风险吗)

  • Mac系统下对U盘进行格式化图文攻略(mac u盘)

    Mac系统下对U盘进行格式化图文攻略(mac u盘)

  • dedecms提示用户名不存在是什么原因造成的(dedecms使用教程)

    dedecms提示用户名不存在是什么原因造成的(dedecms使用教程)

  • 印花税和契税是什么意思?什么时候交?
  • 免税收入怎么做账务处理
  • 税务五化是指
  • 租房抵扣个税计算公式
  • 要交多少增值税
  • 收到失业保险稳岗补贴会计分录
  • 管道燃气工程设计方案
  • 核定征收小规模跟法人有往来账
  • 公务接待和商务服务区别
  • 天猫费率是什么
  • 发票已认证未抵扣怎么办
  • 单位捐赠汽车账务处理要如何做?
  • 补上年所得税加滞纳金入哪个科目?
  • 计税收入金额是什么意思
  • 经济法基础企业所得税好难
  • 医疗机构交企业所得税吗
  • 调用核心征管业务服务节点报错
  • 华为股权激励制度
  • 技术服务合同在北京由哪个法院管辖
  • 企业所得税减免政策2023
  • 增值税发票总金额是含税金额吗
  • 股权代持分红免税吗
  • 自行研发的专利权计入
  • 净利润分配股利的会计分录
  • 投入产出法怎么求逆矩阵
  • 工资薪金怎么申报9
  • 固定资产入账价值计算公式
  • 留抵增值税的会计处理
  • xp系统可以安装cad吗
  • 付给他人押金的会计分录
  • 固定资产内部抵消
  • 微软正在更新office
  • thinkphp autoload 命名空间自定义 namespace
  • 外商投资工业企业有哪些
  • 解决烧心最快方法
  • 安装windows(install windows)
  • jupyter用法
  • 专家坐诊信息
  • 如何在idea上运行python
  • 出口退税率和进项税额
  • 三代手续费支付申请表怎么填
  • 商业保理怎么账务处理?
  • 土地出让金如何缴纳
  • 结转增值税的账务处理
  • 发生了销售交易但没有在销售日记
  • 所得税审核一般需要多久
  • 销售暂估业务处理
  • 基本户转账到法院怎么转
  • 支付兼职人员工资会计处理
  • 主营业务成本和主营业务收入的关系
  • 资本金与注册资金的关系
  • 如何理解应收账款平均余额
  • linux防御
  • centos7服务器配置
  • 管理需要
  • ubuntu 125%
  • win8怎么把c盘恢复出厂设置
  • 删除文件或文件夹时出错怎么办
  • linux修改文件权限
  • Win7系统打开IE提示“堆栈满溢”的多种解决方案
  • win10激活过期有什么影响吗
  • win8打不开咋办
  • uisrollview
  • opengl画实体
  • android canvas scale
  • linux 搜索文件
  • 批处理程序
  • cocos2dx引擎
  • unity hover
  • android javascript 混淆配置。
  • linux重定位命令
  • 从零开始学什么
  • chrome excel
  • jquery控制样式
  • python中计数函数怎么用
  • 文化事业建设税减免政策2023
  • 补农行卡怎么补几天能补完
  • 纳服的拼音
  • 孝感契税缴纳标准
  • 常州市三免卡
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设