注意事项
这玩意很简单,但是我们第一次搞就是搞不成功,为什么呢?因为我们都用的是idea或者eclipse编译。webjar只能在maven上才能打包,所以在使用时,记得maven-clean和maven-package!
先丢代码地址
https://gitee.com/a247292980/lgp20151222
再丢pom.xml
4.0.0 com.lgp webjar 0.0.1-SNAPSHOT jar webjar Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.webjars bootstrap 3.3.7-1 org.webjars jquery 3.1.1 org.springframework.boot spring-boot-maven-plugin
最后丢目录结构
WebjarApplication
没动过
Demo.html
WebJars Demo × Hello, WebJars!
高级技能
/** * 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都耗系统性能的!!!!! **/@Controllerpublic 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); } }}