使用WebJars

Spring Boot框架整合WebJars进行前端静态JavaScript和CSS。

在pom文件中加入二者的依赖文件

<!-- 引用bootstrap -->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>

<!-- 引用jquery -->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>

在src/main/recources/static文件夹下新建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webjars-test</title>
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css"/>
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js">
    </script>

</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>
<script type="text/javascript">
    alert($('.close').attr('href'));
</script>
</html>

在浏览器上访问http://localhost:8080

WebJars还提供了很多其他的依赖,具体使用可以查看WebJars官网(官网地址:https://www.webjars.org/)。

原文地址:https://www.cnblogs.com/ooo0/p/14042488.html