欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring使用webjar

发布时间:2025/7/25 javascript 66 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring使用webjar 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

注意事项

这玩意很简单,但是我们第一次搞就是搞不成功,为什么呢?因为我们都用的是idea或者eclipse编译。webjar只能在maven上才能打包,所以在使用时,记得maven-clean和maven-package!

先丢代码地址

https://gitee.com/a247292980/lgp20151222

再丢pom.xml

<?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.lgp</groupId><artifactId>webjar</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>webjar</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><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></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

最后丢目录结构

WebjarApplication

没动过

Demo.html

<!DOCTYPE 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>

 高级技能

/*** Created by IntelliJ IDEA.* User: a247292980* Date: 2017/08/14
** webjars-locator 包的作用是处理WebJars,省略 webjar 的版本。* 比如对于请求 http://localhost:8080/webjars/jquery/3.1.0/jquery.js省略版本号 3.2.1* 直接使用http://localhost:8080/webjarslocator/jquery/jquery.js也可访问。* 其实吧,webjar,webjarslocator都耗系统性能的!!!!!**/ @Controller public class WebJarController {private final WebJarAssetLocator assetLocator = new WebJarAssetLocator();@ResponseBody@RequestMapping("/webjarslocator/{webjar}/**")public ResponseEntity locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) {try {String mvcPrefix = "/webjarslocator/" + webjar + "/";String mvcPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);String fullPath = assetLocator.getFullPath(webjar, mvcPath.substring(mvcPrefix.length()));return new ResponseEntity(new ClassPathResource(fullPath), HttpStatus.OK);} catch (Exception e) {return new ResponseEntity(HttpStatus.NOT_FOUND);}}}

 

转载于:https://www.cnblogs.com/ydymz/p/8404145.html

总结

以上是生活随笔为你收集整理的Spring使用webjar的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。