Spring Boot整合Bootstrap的超详细步骤

 更新时间:2023年05月08日 11:06:07   作者:wangkay88  
之前做前端开发,在使用bootstrap的时候都是去官网下载,然后放到项目中,在页面引用,下面这篇文章主要给大家介绍了关于Spring Boot整合Bootstrap的超详细步骤,需要的朋友可以参考下

一、添加 Bootstrap 依赖

在 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" 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>

运行程序

访问 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" 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>

在这个页面中,我们使用了 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" 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>

在这个页面中,我们使用了 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" 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 构建页面时,我们还可以使用 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}" 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>

在这个页面中,我们定义了一个 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 的表单和网格系统来创建一个登录表单和一个随机图片。

总结

到此这篇关于Spring Boot整合Bootstrap的超详细步骤的文章就介绍到这了,更多相关SpringBoot整合Bootstrap内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot集成yitter-idgenerator(雪花漂移)分布式Id自增的实现

    SpringBoot集成yitter-idgenerator(雪花漂移)分布式Id自增的实现

    本文主要介绍了SpringBoot集成yitter-idgenerator(雪花漂移)分布式Id自增的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • MyBatis使用标签动态操作数据库详解

    MyBatis使用标签动态操作数据库详解

    这篇文章主要介绍了MyBatis中使用标签动态操作数据库的方法,动态SQL是指在运行PL/SQL块时动态输入SQL语句,是Mybatis的强大特性之⼀,能够完成不同条件下不同的sql拼接,需要的朋友可以参考下
    2024-05-05
  • java中常见XML解析器的使用详解(JAXP,DOM4J,Jsoup,JsoupXPath)

    java中常见XML解析器的使用详解(JAXP,DOM4J,Jsoup,JsoupXPath)

    为了处理和操作XML数据,我们需要使用XML解析器,本文将介绍几种常用的XML解析器,包括JAXP、DOM4J、Jsoup和JsoupXPath,需要的小伙伴可以参考一下
    2023-11-11
  • SpringBoot整合swagger的操作指南

    SpringBoot整合swagger的操作指南

    Swagger 是一个开源的框架,用于设计、构建、文档化和使用 RESTful 风格的 Web 服务,Spring Boot 是一个用于构建独立的、基于生产级别的 Spring 应用程序的框架,本文讲给大家介绍一下SpringBoot整合swagger的操作指南,需要的朋友可以参考下
    2023-09-09
  • Springboot项目Mybatis升级为Mybatis-Plus的详细步骤

    Springboot项目Mybatis升级为Mybatis-Plus的详细步骤

    在许多 Java 项目中,MyBatis 是一个广泛使用的 ORM 框架,然而,随着 MyBatis-Plus 的出现,许多开发者开始迁移到这个更加简洁、高效的工具,它在 MyBatis 的基础上提供了更多的功能,所以本文将介绍Springboot项目Mybatis升级为Mybatis-Plus的详细步骤
    2025-03-03
  • Java透明窗体的设置方法

    Java透明窗体的设置方法

    在本文中我们给大家整理了关于Java透明窗体的设置方法以及需要注意的地方,需要的朋友们学习参考下。
    2019-03-03
  • 通过JDK源码角度分析Long类详解

    通过JDK源码角度分析Long类详解

    这篇文章主要给大家介绍了关于通过JDK源码角度分析Long类的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用long类具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • SpringCloud中Zuul网关原理及其配置

    SpringCloud中Zuul网关原理及其配置

    Spring Cloud是一个基于Spring Boot实现的微服务应用开发工具,其中的Zuul网关可以实现负载均衡、路由转发、鉴权、限流等功能,本文将从Spring Cloud中Zuul网关的原理、使用场景和配置过程详细介绍,帮助大家更好地了解和应用Zuul网关,需要的朋友可以参考下
    2023-06-06
  • SpringBoot3-yaml文件配置方式

    SpringBoot3-yaml文件配置方式

    这篇文章主要介绍了SpringBoot3-yaml文件配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • Java负载均衡服务器实现上传文件同步

    Java负载均衡服务器实现上传文件同步

    这篇文章主要介绍了Java负载均衡服务器实现上传文件同步,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09

最新评论