當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring使用webjar
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PKUWC 2018游记
- 下一篇: cordova开发插件,并在androi