`
rensanning
  • 浏览: 3514357 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37483
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604351
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678121
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87298
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399823
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69078
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90494
社区版块
存档分类
最新评论

Spring Boot 入门 - 基础篇(5)- 使用WebJars

 
阅读更多
WebJars能使Maven的依赖管理支持OSS的JavaScript库/CSS库,比如jQuery、Bootstrap等。

(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
org.springframework.web.servlet.resource.WebJarsResourceResolver
<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
                    ...
  • 大小: 19.2 KB
分享到:
评论
1 楼 www88485400 2017-04-26  
讲的太过简略,让新手弄的话肯定不行。

相关推荐

Global site tag (gtag.js) - Google Analytics