Spring Boot 中使用WebJars


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 
                    ...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM