Spring Boot 中使用WebJars引入javasript依赖

WebJars能使Maven的依赖管理支持OSS的JavaScript库/CSS库,比如jQuery、Bootstrap等;

WebJars是将Web前端Javascript和CSS等资源打包成Java的Jar包,这样在Java Web开发中我们可以借助Maven这些依赖库的管理,保证这些Web资源版本唯一性。

基本原理如下:

With any Servlet 3 compatible container, the WebJars that are in the WEB-INF/lib directory are automatically made available as static resources. 
This works because anything in a META-INF/resources directory in a JAR in WEB-INF/lib is automatically exposed as a static resource.

(1)使用 添加js或者css库
pom.xml

<dependency>  
    <groupId>org.webjars</groupId>  
    <artifactId>bootstrap</artifactId>  
    <version>3.3.7-1</version>  
</dependency>  
<dependency>  
    <groupId>org.webjars</groupId>  
    <artifactId>jquery</artifactId>  
    <version>3.1.1</version>  
</dependency>  

src/main/resources/static/demo.html 

<html>  
    <head>  
        <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>  
        <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>  
        <title>WebJars Demo</title>  
        <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />  
    </head>  
    <body>  
        <div class="container"><br/>  
            <div class="alert alert-success">  
                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>  
                Hello, <strong>WebJars!</strong>  
            </div>  
        </div>  
    </body>  
</html> 

启动应用后可以看到以下log: 

2017-02-09 13:52:48.117  INFO 6188 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : 
Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

启动应用访问 http://localhost:8080/demo.html 

(2)省略版本号

很少在代码中硬编码版本号,所以需要隐藏它

pom.xml添加webjars-locator 

<dependency>  
    <groupId>org.webjars</groupId>  
    <artifactId>webjars-locator</artifactId>  
    <version>0.31</version>  
</dependency>  

src/main/resources/static/demo.html 

<script src="/webjars/jquery/3.1.1/jquery.min.js"></script> 
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script> 
<title>WebJars Demo</title> 
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" /> 

改为

<script src="/webjars/jquery/jquery.min.js"></script> 
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script> 
<title>WebJars Demo</title> 
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" />

启动应用再次访问 http://localhost:8080/demo.html 结果和上边一样

扩展:

引入的开源JavaScript库/CSS库将会以jar的形式被打包进工程! 
spring-boot-demo1-0.0.1-SNAPSHOT.jar\BOOT-INF\lib 

bootstrap-3.3.7-1.jar 
└─ META-INF 
    └─ resources 
        └─ webjars 
            └─ bootstrap 
                └─ 3.3.7-1 
                    ├─ css 
                    |   ├─ bootstrap.min.css 
                    |   ├─ bootstrap.min.css.gz # Gzip文件 
                    ...

jquery-3.1.1.jar └─ META-INF └─ resources └─ webjars └─ jquery └─ 3.1.1 ├─ jquery.min.js ...

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Spring Boot引入jQuery可以通过以下步骤实现: 1. 在Maven或Gradle添加jQuery的依赖Maven: ```xml <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.</version> </dependency> ``` Gradle: ```groovy implementation 'org.webjars:jquery:3.6.' ``` 2. 在HTML页面引入jQuery: ```html <script src="/webjars/jquery/3.6./jquery.min.js"></script> ``` 3. 在JavaScript代码使用jQuery: ```javascript $(document).ready(function() { // jQuery代码 }); ``` 这样就可以在Spring Boot使用jQuery了。 ### 回答2: Spring Boot是一个开源的Java框架,可以快速构建Web应用程序。尽管Spring Boot可以轻松创建基本的Web应用程序,但许多应用程序都需要使用JavaScript库(例如JQuery)。在本文,我们将了解如何在Spring Boot引入jQuery。 首先,我们需要在Spring Boot应用程序添加jQuery的依赖项。Spring Boot使用Maven或Gradle管理依赖项,因此添加jQuery的方式取决于您选择的工具。例如,在Maven,您可以在pom.xml文件加入以下依赖项: ``` <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.0</version> </dependency> ``` 这将向Spring Boot添加WebJars jQuery库的依赖项。 接下来,在您的HTML文件导入jQuery。您可以在HTML添加以下代码: ``` <script src="/webjars/jquery/3.6.0/jquery.min.js"></script> ``` 在这个例子,我们使用WebJars的路径来导入jQuery。这确保了jQuery文件可以正确下载并在我们的应用程序使用。 最后,检查是否成功引入jQuery库。您可以将以下代码添加到HTML的头部: ``` <script> $(document).ready(function() { alert('Hello from jQuery!'); }); </script> ``` 这段代码将在页面加载完成时弹出一个"Hello from jQuery!"的消息框。如果您看到此消息框,则表示成功引入了jQuery库。 在Spring Boot应用程序引入jQuery非常简单。通过添加jQuery的依赖项,导入jQuery的路径,然后检查是否已成功引入jQuery库,您将能够在应用程序轻松使用jQuery来开发复杂的Web应用程序。 ### 回答3: Spring Boot是一个快速开发的框架,它提供了很多便利性,可以大大减少开发的时间和复杂度。在实际开发,前端开发不可避免的会用到jquery。本文将介绍如何在Spring Boot引入jquery。 一般来说,我们可以在html页面通过script标签引入jquery的js文件,但在Spring Boot,我们可以通过Maven或Gradle来引入jquery的依赖。这样可以让我们的工程更加的清晰和规范,同时也可以避免由于文件引用路径不正确导致的问题。 以Maven为例,我们可以在pom.xml添加以下依赖: ``` <!--引入jquery--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.0</version> </dependency> ``` 这样我们就成功引入了jquery的依赖。在html页面,我们可以通过以下方式引入jquery的js文件: ``` <!-- 引入jQuery --> <script src="/webjars/jquery/3.6.0/jquery.min.js"></script> ``` 在Gradle,我们可以在build.gradle添加以下依赖: ``` dependencies { // 引入jquery implementation 'org.webjars:jquery:3.6.0' } ``` 在html页面同样可以通过以下方式引入jquery的js文件: ``` <!-- 引入jQuery --> <script src="/webjars/jquery/3.6.0/jquery.min.js"></script> ``` 以上就是在Spring Boot项目引入jQuery的基本步骤。当然,我们也可以通过CDN等方式来引入jquery的js文件,这里只是一个简单的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值