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

  • 微博营销中怎样提高微博转发率(微博营销怎样有亮点)

    微博营销中怎样提高微博转发率(微博营销怎样有亮点)

  • 小米12是直屏还是曲屏(小米12还是曲面屏吗)

    小米12是直屏还是曲屏(小米12还是曲面屏吗)

  • hdm2没信号是什么意思(hdm1无信号)

    hdm2没信号是什么意思(hdm1无信号)

  • 荣耀9x对比oppok3(荣耀9X对比OppOK9Ⅹ)

    荣耀9x对比oppok3(荣耀9X对比OppOK9Ⅹ)

  • 微信视频铃声怎么设置自己喜欢的音乐(微信视频铃声怎么设置自己喜欢的歌曲)

    微信视频铃声怎么设置自己喜欢的音乐(微信视频铃声怎么设置自己喜欢的歌曲)

  • 手机互联映射用处大吗(手机互联映射用流量吗)

    手机互联映射用处大吗(手机互联映射用流量吗)

  • 华为mate30是指纹解锁吗

    华为mate30是指纹解锁吗

  • 苹果x刘海屏红点闪烁(苹果刘海屏红灯一直在闪烁)

    苹果x刘海屏红点闪烁(苹果刘海屏红灯一直在闪烁)

  • 视频号内测资格有什么用(视频号内测资格怎么获得)

    视频号内测资格有什么用(视频号内测资格怎么获得)

  • 淘宝退货什么时候能把钱退回来(淘宝退货什么时候退款到账)

    淘宝退货什么时候能把钱退回来(淘宝退货什么时候退款到账)

  • vivox9有没有录屏功能(vivox9手机录屏)

    vivox9有没有录屏功能(vivox9手机录屏)

  • 手机不在身边微信收款语音播报怎么设置(手机不在身边微信怎么上)

    手机不在身边微信收款语音播报怎么设置(手机不在身边微信怎么上)

  • 打电话有视频广告怎么回事(打电话有视频广告)

    打电话有视频广告怎么回事(打电话有视频广告)

  • 电脑适配器未连接是什么意思(电脑适配器未连接什么意思)

    电脑适配器未连接是什么意思(电脑适配器未连接什么意思)

  • 华为的曲面屏容易坏吗(华为曲面屏容易碎怎么办)

    华为的曲面屏容易坏吗(华为曲面屏容易碎怎么办)

  • 抖音如何开启合集功能(抖音怎么开启合唱)

    抖音如何开启合集功能(抖音怎么开启合唱)

  • i7 6700是几核处理器(i7 6700核显相当于什么显卡)

    i7 6700是几核处理器(i7 6700核显相当于什么显卡)

  • 手机qq怎么一键删除说说(手机qq怎么一键清好友)

    手机qq怎么一键删除说说(手机qq怎么一键清好友)

  • vue怎么把字体变大(怎么改变vue里字体的大小)

    vue怎么把字体变大(怎么改变vue里字体的大小)

  • 通常说cpu是指(通常所说的cpu)

    通常说cpu是指(通常所说的cpu)

  • wps手机版怎么删去一页(wps手机版怎么删除ppt中的一页)

    wps手机版怎么删去一页(wps手机版怎么删除ppt中的一页)

  • xsmax美版att支持电信吗

    xsmax美版att支持电信吗

  • 手机串号怎么查(华为手机串号怎么查)

    手机串号怎么查(华为手机串号怎么查)

  • 苹果序列号泄漏了咋办(苹果序列号泄漏怎么补救)

    苹果序列号泄漏了咋办(苹果序列号泄漏怎么补救)

  • 手机怎么下载爱思助手(手机怎么下载爱奇艺)

    手机怎么下载爱思助手(手机怎么下载爱奇艺)

  • oppok3升降摄像头怎么用(oppok3升降摄像头黑屏)

    oppok3升降摄像头怎么用(oppok3升降摄像头黑屏)

  • 抖音输液输入怎么做(抖音输液金钱特效图片)

    抖音输液输入怎么做(抖音输液金钱特效图片)

  • day53-马踏棋盘(马踏棋盘游戏规则)

    day53-马踏棋盘(马踏棋盘游戏规则)

  • 电子缴款凭证和完税证明都可以做原始凭证吗
  • 进项票管理软件
  • 税金及附加包括印花税吗
  • 小规模纳税人怎么变成一般纳税人
  • 公司注销后持股要交税吗
  • 投资性房地产发生减值迹象均要计提减值准备
  • 增值税普通发票和专用发票有什么区别
  • 费用怎么分析
  • 期末资产负债
  • 母子公司间提供保洁保安合法吗
  • 私对公转错了多久退回来
  • 年终结账后,应当更换新账的有( )
  • 增值税发票可以抵税吗
  • 增值税发票进项抵扣是什么意思
  • 什么情况下税务会查账
  • 旧的活动板房多少钱一个
  • 委托付款分录
  • 税前利润弥补亏损
  • 技术服务收入和产品服务收入举个例子
  • linux怎么操作
  • 网络命令netstat
  • win10重装系统后网卡没了
  • 当地街道
  • 使用php进行mysql数据库编程的基本步骤
  • 接受捐赠会计准则
  • 大雾笼罩的早晨
  • 外币交易汇兑损益包括
  • 企业所得税征收方式有哪些?
  • 又拍云使用教程
  • js返回上一步操作
  • php运用
  • 投资性房地产抵债差额计入
  • 银行代发工资会扣个人所得税吗
  • sqlserver 死锁查询
  • 怎么摊销租金
  • 办公室购买电脑计入
  • 佣金的会计如何计算
  • 小微企业增值税减免政策
  • 材料采购的账务怎么处理
  • 应付账款的入账价值应当包括增值税的进项税额
  • 备案办税人员信息是谁
  • 报销发票哪些可以抵税
  • 小规模纳税人企业所得税怎么算
  • 员工垫付款报销单
  • 积分购物骗局
  • 预提房租会计分录怎么填
  • 员工缴纳工伤保险怎么赔偿
  • 研发费用加计扣除的条件
  • 其他应收款核算什么
  • 小规模纳税人购进商品会计分录
  • 小规模附加税要交吗
  • 企业优惠政策是什么意思
  • 现金存银行凭证
  • 计提有哪些科目
  • 研发收入超过研发成本
  • 企业工资薪金支出怎么确认
  • 因windowssystem32
  • Ubuntu系统中文输入法
  • winpe下载u盘启动版
  • main.exe是什么意思
  • 软件无法卸载怎么办,安装的软件卸载不了怎么办
  • win7网卡驱动怎么卸载
  • win7开启远程设置
  • 电脑跳出windows 许可
  • linux如何用
  • win7如何更改欢迎界面
  • win8系统教程
  • 万能win8pe工具箱怎么用
  • ,linux
  • Metaio in Unity3d 教程---Metaio扫描图片要求(Image Tracking)
  • android四大组件生命周期
  • android加载dex
  • shell脚本怎么导出
  • jquery使用css
  • bash fi
  • jquery常见问题
  • js实现滑动效果
  • 说几条javascript的基本规范
  • 企业所得税税率10%
  • 税收优惠政策有经营和居民住宅出租
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设