1. 添加资源处理
  1. package org.springframework.boot.autoconfigure.web.servlet.
  2. public class WebMvcAutoConfiguration {
  1.   private final ResourceProperties resourceProperties;
  1.   public void addResourceHandlers(ResourceHandlerRegistry registry) {
  2. if (!this.resourceProperties.isAddMappings()) {
  3. logger.debug("Default resource handling disabled");
  4. } else {
  5. Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
  6. CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
  7. if (!registry.hasMappingForPattern("/webjars/**")) {
  8. this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"})//解析此路径下的静态资源文件
                  .addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"})          //解析此路径下的静态资源文件
                  .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
  9. }
  10.  
  11. String staticPathPattern = this.mvcProperties.getStaticPathPattern();
  12. if (!registry.hasMappingForPattern(staticPathPattern)) {
  13. this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern})
                  .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                  .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
  14. }
  15. }
  16. }
  17.   }

欢迎页

  1.     @Bean
  2. public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
  3. return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext),
               applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());//1.欢迎页所在的静态资源访问根路径:/**
  4. }
  5.  
  6. static String[] getResourceLocations(String[] staticLocations) {
  7. String[] locations = new String[staticLocations.length + WebMvcAutoConfiguration.SERVLET_LOCATIONS.length];
  8. System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
  9. System.arraycopy(WebMvcAutoConfiguration.SERVLET_LOCATIONS, 0, locations, staticLocations.length, WebMvcAutoConfiguration.SERVLET_LOCATIONS.length);
  10. return locations;
  11. }
  12.  
  13. private Optional<Resource> getWelcomePage() {
  14. String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());//2.静态页所在的根路径下的路径
  15. return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
  16. }
  17.  
  18. private Resource getIndexHtml(String location) {
  19. return this.resourceLoader.getResource(location + "index.html");//3.欢迎页的默认文件名叫index.html
  20. }
  1. //1.欢迎页所在的静态资源访问根路径:/**

  1. //2.静态页所在的根路径下的路径
  1. @ConfigurationProperties(
  2. prefix = "spring.resources",
  3. ignoreUnknownFields = false
  4. )
  5. public class ResourceProperties {
  6. private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
                 new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};//静态页路径
  7. private String[] staticLocations;
  8. private boolean addMappings;
  9. private final ResourceProperties.Chain chain;
  10. private final ResourceProperties.Cache cache;
  11.  
  12. public ResourceProperties() {
  13. this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
  14. this.addMappings = true;
  15. this.chain = new ResourceProperties.Chain();
  16. this.cache = new ResourceProperties.Cache();
  17. }
  18.  
  19. public String[] getStaticLocations() {
  20. return this.staticLocations;
  21. }

  

   

欢迎页

                   

1、WebJars介绍

Web前端使用了越来越多的JS或CSS,如jQuery,Backbone.js和Bootstrap。一般情况下,我们是将这些Web资源拷贝到Java Web项目的webapp相应目录下进行管理。这种通过人工方式管理可能会产生版本误差,拷贝版本错误,漏拷等现象,导致前端页面无法正确展示,版本不一致,文件混乱等,导致出现一些莫名其妙的错误等。

WebJars是将web前端资源(js,css等)打成jar包文件,然后借助Maven工具,以jar包形式对web前端资源进行统一依赖管理,保证这些Web资源版本唯一性。WebJars的jar包部署在Maven中央仓库上。

WebJars官网:https://www.webjars.org/

2、SpringBoot使用WebJars

  1.  
  2. 页面图标
  1. public class WebMvcAutoConfiguration {
  2.      @Configuration
  3. @ConditionalOnProperty(
  4. value = {"spring.mvc.favicon.enabled"},
  5. matchIfMissing = true
  6. )
  7. public static class FaviconConfiguration implements ResourceLoaderAware {
  8. private final ResourceProperties resourceProperties;
  9. private ResourceLoader resourceLoader;
  10.  
  11. public FaviconConfiguration(ResourceProperties resourceProperties) {
  12. this.resourceProperties = resourceProperties;
  13. }
  14.  
  15. public void setResourceLoader(ResourceLoader resourceLoader) {
  16. this.resourceLoader = resourceLoader;
  17. }
  18.  
  19. @Bean
  20. public SimpleUrlHandlerMapping faviconHandlerMapping() {
  21. SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
  22. mapping.setOrder(-2147483647);
  23. mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
  24. return mapping;
  25. }
  26.  
  27. @Bean
  28. public ResourceHttpRequestHandler faviconRequestHandler() {
  29. ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
  30. requestHandler.setLocations(this.resolveFaviconLocations());
  31. return requestHandler;
  32. }
  33.  
  34. private List<Resource> resolveFaviconLocations() {、
  35.           //"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
  36.            String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
  37. List<Resource> locations = new ArrayList(staticLocations.length + 1);
  38. Stream var10000 = Arrays.stream(staticLocations);
  39. ResourceLoader var10001 = this.resourceLoader;
  40. this.resourceLoader.getClass();
  41. var10000.map(var10001::getResource).forEach(locations::add);
  42. locations.add(new ClassPathResource("/"));
  43. return Collections.unmodifiableList(locations);
  44. }
  45. }
  46. }

  1. 自定义静态资源路径
  1.  
  1. <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.css}" rel="stylesheet">
  2. <link href="asserts/css/dashboard.css" th:href="@{/asserts/css/dashboard.css}" rel="stylesheet">
  3. <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js" th:src="@{/webjars/jquery/3.3.1-1/jquery.js}"></script>
  4. <script type="text/javascript" src="asserts/js/popper.min.js" th:src="@{/webjars/popper.js/1.14.4/popper.js}"></script>
  5. <script type="text/javascript" src="asserts/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/4.1.3/js/bootstrap.js}"></script>
  6. <script type="text/javascript" src="asserts/js/feather.min.js" th:src="@{/asserts/js/feather.min.js}"></script>
  7. <script type="text/javascript" src="asserts/js/Chart.min.js" th:src="@{/asserts/js/Chart.min.js}"></script>
  1. 如果不写thsrc 或者th:href 去连接获取,若URL请求路径是多级的,eg:http://localhost:8080/emp/add
    那么资源文件就会去/emp/asserts/js/下去寻找。寻找不到就会报错了。

7.SpringBoot 之 Web的更多相关文章

  1. SpringBoot的Web开发

    一.创建Web项目 创建的时候勾选对应web选项即可,会自动引入相应的starter,pom如下: <dependency> <groupId>org.springframew ...

  2. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  3. springBoot 搭建web项目(前后端分离,附项目源代码地址)

    springBoot 搭建web项目(前后端分离,附项目源代码地址)   概述 该项目包含springBoot-example-ui 和 springBoot-example,分别为前端与后端,前后端 ...

  4. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  5. SpringBoot日记——Web开发篇

    准备开始实战啦!~~~~ 我们先来看,SpringBoot的web是如何做web开发的呢?通常的步骤如下: 1.创建springboot应用,指定模块: 2.配置部分参数配置: 3.编写业务代码: 为 ...

  6. ②SpringBoot之Web综合开发

    Spring boot初级教程 :<SpringBoot入门教学篇①>,方便大家快速入门.了解实践Spring boot特性,本文介绍springBoot的web开发 web开发sprin ...

  7. SpringBoot:Web开发

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 , 基于atguigu 1.5.x 视频优化 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处 ...

  8. 基于springboot的web项目最佳实践

    springboot 可以说是现在做javaweb开发最火的技术,我在基于springboot搭建项目的过程中,踩过不少坑,发现整合框架时并非仅仅引入starter 那么简单. 要做到简单,易用,扩展 ...

  9. SpringBoot 基于web应用开发(请求参数获取,静态资源,webjars)

    SpringBoot 基于web应用开发 一.Lombok使用 1.导入依赖库 <dependency>    <groupId>org.projectlombok</g ...

  10. springboot 整合 web 项目找不到 jsp 文件

    今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...

随机推荐

  1. MySQL分区和分表

    一.概念 1.为什么要分表和分区?日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询 ...

  2. Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析

    Hashtable 是一个很常见的数据结构类型,前段时间阿里的面试官说只要搞懂了HashTable,hashMap,HashSet,treeMap,treeSet这几个数据结构,阿里的数据结构面试没问 ...

  3. JDK8字符串拼接的正确姿势

    1. 对列表中的元素进行拼接 以前,对一个列表中的字符串进行拼接时,常见的代码如示例1所示: 代码示例1 List<String> ids = ImmutableList.of(" ...

  4. codeforces742B

    Arpa’s obvious problem and Mehrdad’s terrible solution CodeForces - 742B There are some beautiful gi ...

  5. BZOJ2152[国家集训队]聪聪可可——点分治

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...

  6. Java监听器Listener的使用详解

    监听器用于监听Web应用中某些对象的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当监听范围的对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计网站在线人数.系 ...

  7. Java 的类加载机制

    Java 的类加载机制 来源 https://www.cnblogs.com/xiaoxi/p/6959615.html 一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内 ...

  8. 【题解】 bzoj3894: 文理分科 (网络流/最小割)

    bzoj3894,懒得复制题面,戳我戳我 Solution: 首先这是一个网络流,应该还比较好想,主要就是考虑建图了. 我们来分析下题面,因为一个人要么选文科要么选理科,相当于两条流里面割掉一条(怎么 ...

  9. [算法进阶0x10]基本数据结构A作业总结

    在线题目\(oj\)评测地址:https://xoj.red/contests/show/1237 T1-Editor(hdu4699) 题目描述 维护一个整数序列的编辑器,有以下5种操作,操作总数不 ...

  10. 洛谷 P4097 [HEOI2013]Segment 解题报告

    P4097 [HEOI2013]Segment 题目描述 要求在平面直角坐标系下维护两个操作: 在平面上加入一条线段.记第 \(i\) 条被插入的线段的标号为 \(i\) 给定一个数 \(k\),询问 ...