目录
摘要
本文介绍springboot项目中引入webjars的方法
一、创建webjars
1. 创建maven项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>config-ui</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.versin>1.8</java.versin>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.outputDirectory}/META-INF/resources/webjars</targetPath>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>nexus-aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
2. 编写网页
在resources目录下新建static目录,用来存放静态网页与资源。
二、使用webjar
1. 引入jar包
将上一步骤的jar包引入到项目中,此处我已经install到了本地,只需要在pom中引入相应的依赖即可。
<dependency>
<groupId>com.demo</groupId>
<artifactId>config-ui</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2. 配置资源
用到的StreamUtils.java源码
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/system-monitor/**").addResourceLocations("classpath:/META-INF/resources/webjars/static/");
}
}
3. 测试效果
启动项目,访问http://localhost:8080/system-monitor/index.html,即可看到jar包中的静态网页。