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

  • 应纳税所得额是什么意思
  • 增值税普票税额怎么算出来的
  • 增值税发票注明金额是含税还是不含税
  • 收到固定资产发票抵扣进项税
  • 国内支付业务收入怎么算
  • 税务大厅可以开票吗
  • 小规模纳税人年应税销售额标准是
  • 高新技术企业发展现状
  • 跨期差额扣除的计算开票以及账务处理
  • 商誉转销会计分录
  • 承兑汇票兑现怎么填写
  • 企业收不回款项计入什么科目?
  • 工资薪金支出怎么调整
  • 客户给现金
  • 专票可以当费用票吗
  • 采购退货退款怎么做账
  • 电子发票一定要入账吗
  • 增值税零申报,企业所得税会有税额吗
  • 采购原材料运费会计分录
  • 应交增值税出口退税科目怎么结平
  • 个体工商户经营所得税怎么申报
  • 股东房产用于公司经营的是否交房产税
  • 营运资金投资额计算公式
  • 开票的时候开票人是管理员
  • 购买原材料暂估入账的会计分录
  • 经营租赁中出租人发生的初始直接费用是指
  • 利润出现负数怎么调整
  • 个人收回转让的股权个税应如何处理?
  • 个体工商户未给员工缴纳社保
  • 出租人负责维修
  • php如何实现伪静态
  • 公司收入算认缴出资吗
  • 经营出租的固定资产折旧计入哪里
  • “Property or method “***“ is not defined on the instance but referenced during render.”报错的原因及解决方案
  • 递延所得税资产和负债怎么理解
  • 库存现金盘亏会计分录无法查明原因
  • 待安置期间生活补助费多少钱
  • 西部大开发税收优惠政策是什么时候开始的?
  • IIS 7.5 asp Session超时时间设置方法
  • react_router
  • 监事和财务负责人可以一个人么
  • 购物车html模板
  • 用html语言完成以下内容
  • 小规模纳税人房产税优惠政策2023
  • 什么是免抵调库税额
  • 处置固定资产科目
  • pythonsorted函数的作用
  • 母公司给子公司借款要利息吗
  • 第四季度报表和年报对不上
  • 土地增值税的扣除项目金额有哪些
  • access里面也暗含了sql
  • db2数据文件
  • 会计从业资格证取消了吗
  • 广告费的税额计入哪里
  • 借款怎么入会计分录
  • 出售转让固定资产的账务处理
  • 预付下个月租金分录
  • 结转本月福利费会计分录
  • 企业中征码怎么生成
  • 违反会计法规定的行为有哪些
  • 安装fedora33
  • Win10 Mobile RS2预览版14926已知问题和解决方法汇总 谨慎升级
  • hptasks.exe是病毒吗 是什么进程 hptasks进程说明
  • keyword是啥
  • win10未检测到任何网络硬件
  • win7 64位旗舰版系统联网时提示0x80070002的解决方法
  • Linux安装配置jdk
  • 环境篇作文
  • linux命令-s
  • perl中my
  • Javascript Throttle & Debounce应用介绍
  • javascript点击切换div内容
  • typeof的缺点
  • jquery的过滤器用于指定什么东西
  • javascript常用类型
  • spring mvc jsp
  • android 材料设计
  • 对超市的建议和意见怎么写简短
  • 什么是增值税免抵税额
  • 2018年申报时间
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设