WebJars是将这些通用的Web前端资源打包成Java的Jar包,然后借助Maven工具对其管理,保证这些Web资源版本唯一性,升级也比较容易。关于webjars资源,有一个专门的网站http://www.webjars.org/,我们可以到这个网站上找到自己需要的资源,在自己的工程中添加入maven依赖,即可直接使用这些资源了
1. 使用
添加依赖
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>2.1.1</version> </dependency> |
2. 打包:
1 2 3 4 5 6 7 8 | <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <failOnMissingWebXml> false </failOnMissingWebXml> </configuration> </plugin> |
3. 引用
a.静态页面:
1 2 3 4 5 6 | <link rel= 'stylesheet' href= 'webjars/bootstrap/2.1.1/css/bootstrap.min.css' > <script type= 'text/javascript' src= 'webjars/jquery/1.8.2/jquery.min.js' ></script> <script type= 'text/javascript' src= 'webjars/bootstrap/2.1.1/js/bootstrap.min.js' ></script> <script type= 'text/javascript' src= 'webjars/ajax-form/2.1.3/ajax-form.js' ></script> |
b.jsp
1 2 3 4 5 6 7 8 9 | <link rel= 'stylesheet' href= '<%= org.webjars.AssetLocator.getWebJarPath("css/bootstrap.min.css") %>' > <script type= 'text/javascript' src= '<%= org.webjars.AssetLocator.getWebJarPath("jquery.min.js") %>' ></script> <script type= 'text/javascript' src= '<%= org.webjars.AssetLocator.getWebJarPath("js/bootstrap.min.js") %>' ></script> <script type= 'text/javascript' src= '<%= org.webjars.AssetLocator.getWebJarPath("ajax-form.js") %>' ></script> |