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
...
Spring Boot 中使用WebJars的更多相关文章
- Spring Boot中使用 Spring Security 构建权限系统
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- Spring Boot中的事务管理
原文 http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...
- Spring Boot中的注解
文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...
- 在Spring Boot中使用Https
本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- springboot(十一):Spring boot中mongodb的使用
mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...
- springboot(三):Spring boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
随机推荐
- java struts2入门学习---国际化
一.国际化的概念 1.不同国家的人访问同一个网站,显示的语言不同. 2.对JSP页面进行国际化 属性(properties)文件命名规则:基名---语言--国家如, message_zh_CN.pro ...
- url 中非法字符替换,java 正则替换
url在传输时不允许的一些字符串,参考自:http://www.ietf.org/rfc/rfc1738.txt 以下字符用java正则替换为"_",一句话搞定: "{& ...
- [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。
一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...
- MYSQL 5.5.32的单机多实例部署
Centos6.6安装并配置单机多实例的MYSQL数据库 本文介绍安装单机多实例的MYSQL数据库的环境如下: 系统平台环境:Centos6.6 Mysql软件包:Mysql-5.5.32.tar.g ...
- 【jsp】配置错误页面
1,使用JSP方式 如果配置是Jsp时,需要把isErrorPage设置为true, 以及设置 <%@ page language="Java" contentType=&q ...
- 打开Word时出现“The setup controller has encountered a problem during install. Please ...”什么意思
解决办法:找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文件夹删除或改名,就 ...
- mac mini 制作fusion drive 的方法
下载yosemite,格式化磁盘,运行如下命令,制作os x 启动盘 sudo /Applications/Install\ OS\ X\ Yosemite.app/Contents/Resour ...
- Shell脚本开发规范
一.前言 由于工作需要,最近重新开始拾掇shell脚本.虽然绝大部分命令自己平时也经常使用,但是在写成脚本的时候总觉得写的很难看.而且当我在看其他人写的脚本的时候,总觉得难以阅读.毕竟shell脚本这 ...
- SQL Server 索引重建脚本
在数据的使用过程中,由于索引page碎片过多,带来一些不利的性能问题,我们有时候需要对数据库中的索引进行重组或者重建工作.通常这个阈值为30%,大于30%我们建议进行索引重建,小于则进行重组操作.以下 ...
- U811.1接口EAI系列之一-通用访问EAI方法--统一调用EAI公共方法--VB语言
1.现在做的项目是关于业务系统与U811.1的接口项目. 2.那么就需要调整通过EAI调用生成U8业务单据. 3.下面就一个通用的向U8-EAI传递XML的通用方法 4.肯定有人会问怎么还用VB调用呢 ...