zoukankan      html  css  js  c++  java
  • Spring Boot 中使用WebJars

     
    ajax 参数说明
     

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

    复制代码
    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 
                        ...
    复制代码
     
    标签: SpringBoot
  • 相关阅读:
    PHP读取XML数据中CDATA内数值
    微信支付报ip错,怀疑是因为不能正确获取$_Server[addr])ip导致的
    微信支付错误两个问题的解决:curl出错,错误码:60
    tp框架 验证码的应用注意事项
    PHP结合jQuery.autocomplete插件实现输入自动完成提示的功能
    mysql存储小数
    windows服务器剪贴板不能共用的解决办法
    网页设为首页和添加收藏
    网页qq客服代码并自定义图片
    windows apache开启url rewrite
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/13452482.html
Copyright © 2011-2022 走看看