注意事项

这玩意很简单,但是我们第一次搞就是搞不成功,为什么呢?因为我们都用的是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相关推荐

  1. Spring Boot 实践折腾记(20):Thymleaf + webjar + ECharts 构建本地图表

    前言 作为geek后端程序员,画图的选择其实有很多,手画,excel,ppt,copy别人的图等.虽然用excel最方便,但是类似按中国省份来显示详细数据的图,excel是画不了的,而PPT画起来又很 ...

  2. 【spring boot】第3篇:spring boot 进行 web 开发

    spring boot 对 web 静态资源的管理 经过前两篇文章我们知道spring boot最大的功能就是自动配置.主要是通过以下两个类实现该功能: xxxxAutoConfiguration : ...

  3. springboot的jsp应该放在哪_web项目jsp放在哪里 Spring Boot 静态资源处理(4)

    Spring Boot 默认将 /webjars/** 映射到 classpath:/META-INF/resources/webjars/ ,结合我们上面讲到的访问资源的规则,便可以得知我们在JSP ...

  4. Spring、Spring MVC、Spring Boot三者的关系还傻傻分不清楚?

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 在本文中,你将获取到有关于Spring,Spring MVC和Spring Boot的概述, ...

  5. 使用 Spring Boot 快速构建 Spring 框架应用

    https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/index.html Spring 框架对于很多 Java 开发人员来说都不陌生 ...

  6. 使用 Spring Boot 快速构建 Spring 框架应用--转

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...

  7. Spring Boot实战系列《六》:人事管理系统的登录设计

    Spring Boot实战系列<六>:人事管理系统的登录设计 Spring Boot实战系列<六>:人事管理系统的登录设计 1.前言 在上一篇中教大家在IEDA或者eclips ...

  8. Spring boot的Webjars

    Webjars Spring boot 以jar包的方式,引入静态资源 创建Web工程 WebMvcAutoConfiguration Ctrl+Shift+R 搜索addResourceHandle ...

  9. springboot 之Spring Web Mvc Framework

    1.SpringMvc自动配置 Spring Boot 为SpringMvc提供了自动配置. 自动配置包含Spring的以下特征: (1)视图解析器ContentNegotiatingViewReso ...

最新文章

  1. POJ 2420 A Star not a Tree?【爬山法】
  2. smokeping部署安装
  3. matlab工具箱设置学习率_MATLAB金融工具箱:06:统计套利的机器学习2:特征工程和模型开发...
  4. php使用redis持久化,redis是如何持久化的
  5. SAP UI5 应用 index.html 里 data-sap-ui-resourceroots 指令的含义和作用
  6. 频域分析方法的理解(信号、频谱、能量谱、功率谱、倒频谱、小波分析)(终极教程)
  7. shell编程入门 linux解释器原理,Shell编程入门Linux解释器原理详细介绍 使用Shell进行工作的人们对.doc...
  8. 计算机四级考试题数据库,计算机四级考试《数据库系统工程师》试题及答案
  9. html显示当前时间_HTML基础教程:超链接的使用
  10. 爬虫必备技能xpath的用法和实战
  11. 「代码随想录」322. 零钱兑换 【动态规划】力扣详解!
  12. 最速下降法解析(理解笔记)
  13. 马哥教育python视频百度云_马哥教育python基础语法全讲解视频资料分享
  14. Office Open XML 文档格式(转)
  15. BScroll切换才能滚动,刷新一下就不能滚动
  16. 安装一台RHEL7虚拟机
  17. 系列学习 Gateway 之第 5 篇 —— 搭建高可用网关(完结)
  18. php api地址解析,百度地图API-根据地址解析坐标,根据坐标解析地址
  19. iOS 加粗字体方法 (不改变字体字号只加粗文字)
  20. IntelliJ IDEA - 自定义 TODO

热门文章

  1. SpringBoot jar包不支持jsp
  2. sql查询每个学生的最高成绩mysql语句
  3. 代码实现把字符串反转
  4. [Wordpress]wp_dropdown_categories() 添加自定义的attribute(属性)
  5. 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (第九节)
  6. Java学习笔记之:Java JDBC
  7. 1280*720P和1920*1080P的视频在25帧30帧50帧60帧时的参数
  8. python第五章课后题答案超星_MOOC网课章节答案_Python 语言程序设计见面课测试答案...
  9. Apache Hook机制解析(下)——实战:在自己的代码中使用Apache的钩子
  10. nginx反向代理监听非80端口造成的端口丢失解决方案