北京网站建设报价明细,网店装修教程免费,网站定制公司kinglink,重庆网站的网络推广Web开发探究
简介 其实SpringBoot的东西用起来非常简单#xff0c;因为SpringBoot最大的特点就是自动装配 使用SpringBoot的步骤#xff1a;
1、创建一个SpringBoot应用#xff0c;选择我们需要的模块#xff0c;SpringBoot就会默认将我们的需要的模块自动配置好
2、手动…Web开发探究
简介 其实SpringBoot的东西用起来非常简单因为SpringBoot最大的特点就是自动装配 使用SpringBoot的步骤
1、创建一个SpringBoot应用选择我们需要的模块SpringBoot就会默认将我们的需要的模块自动配置好
2、手动在配置文件中配置部分配置项目就可以运行起来了
3、专注编写业务代码不需要考虑以前那样一大堆的配置了。
要熟悉掌握开发之前学习的自动配置的原理一定要搞明白
比如SpringBoot到底帮我们配置了什么我们能不能修改我们能修改哪些配置我们能不能扩展
向容器中自动配置组件 *** Autoconfiguration自动配置类封装配置文件的内容***Properties
静态资源处理
静态资源映射规则
首先我们搭建一个普通的SpringBoot项目回顾一下HelloWorld程序 写请求非常简单那我们要引入我们前端资源我们项目中有许多的静态资源比如cssjs等文件这个SpringBoot怎么处理呢 如果我们是一个web应用我们的main下会有一个webapp我们以前都是将所有的页面导在这里面的对吧但是我们现在的pom呢打包方式是为jar的方式那么这种方式SpringBoot能不能来给我们写页面呢当然是可以的但是SpringBoot对于静态资源放置的位置是有规定的
我们先来聊聊这个静态资源映射规则 SpringBoot中SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面 我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法 有一个方法addResourceHandlers 添加资源处理 Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {// 已禁用默认资源处理logger.debug(Default resource handling disabled);return;}// 缓存控制Duration cachePeriod this.resourceProperties.getCache().getPeriod();CacheControl cacheControl this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();// webjars 配置if (!registry.hasMappingForPattern(/webjars/**)) {customizeResourceHandlerRegistration(registry.addResourceHandler(/webjars/**).addResourceLocations(classpath:/META-INF/resources/webjars/).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}// 静态资源配置String staticPathPattern this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}
}读一下源代码比如所有的 /webjars/** 都需要去 classpath:/META-INF/resources/webjars/ 找对应的资源
什么是webjars 呢
Webjars本质就是以jar包的方式引入我们的静态资源 我们以前要导入一个静态资源文件直接导入即可。
第一种静态资源映射规则
使用SpringBoot需要使用Webjars我们可以去搜索一下
网站https://www.webjars.org
要使用jQuery我们只要要引入jQuery对应版本的pom依赖即可
dependencygroupIdorg.webjars/groupIdartifactIdjquery/artifactIdversion3.4.1/version
/dependency导入完毕查看webjars目录结构并访问Jquery.js文件 访问只要是静态资源SpringBoot就会去对应的路径寻找资源我们这里访问http://localhost:8080/webjars/jquery/3.4.1/jquery.js 第二种静态资源映射规则常用
1、那我们项目中要是使用自己的静态资源该怎么导入呢我们看下一行代码 2、我们去找staticPathPattern发现第二种映射规则 /** , 访问当前的项目任意资源它会去找 resourceProperties 这个类我们可以点进去看一下分析
// 进入方法
public String[] getStaticLocations() {return this.staticLocations;
}
// 找到对应的值
private String[] staticLocations CLASSPATH_RESOURCE_LOCATIONS;
// 找到路径
private static final String[] CLASSPATH_RESOURCE_LOCATIONS { classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/
};3、ResourceProperties 可以设置和我们静态资源有关的参数这里面指向了它会去寻找资源的文件夹即上面数组的内容。
4、所以得出结论以下四个目录存放的静态资源可以被我们识别
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/5、我们可以在resources根目录下新建对应的文件夹都可以存放我们的静态文件 6、比如我们访问 http://localhost:8080/1.js , 他就会去这些文件夹中寻找对应的静态资源文件 自定义静态资源路径知道这种方式就行一般不会用
我们也可以自己通过配置文件来指定一下哪些文件夹是需要我们放静态资源文件的在application.properties中配置
spring.resources.static-locationsclasspath:/coding/,classpath:/ss/总结
在springboot我们可以使用以下方式处理静态资源 webjars localhost:8080/webjars/publicstatic/**resources localhost:8080/ 优先级resources static默认 public
首页处理
静态资源文件夹说完后我们继续向下看源码可以看到一个欢迎页的映射就是我们的首页
Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {WelcomePageHandlerMapping welcomePageHandlerMapping new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),this.mvcProperties.getStaticPathPattern());welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());return welcomePageHandlerMapping;
}点进去继续看
private OptionalResource getWelcomePage() {String[] locations getResourceLocations(this.resourceProperties.getStaticLocations());// ::是java8 中新引入的运算符// Class::function的时候function是属于Class的应该是静态方法。// this::function的funtion是属于这个对象的。// 简而言之就是一种语法糖而已是一种简写return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
// 欢迎页就是一个location下的的 index.html 而已
private Resource getIndexHtml(String location) {return this.resourceLoader.getResource(location index.html);
}截图说明 欢迎页静态资源文件夹下的所有 index.html 页面被 /** 映射。 比如我访问 http://localhost:8080/ 就会找静态资源文件夹下的 index.html 新建一个 index.html 在我们上面的3个目录( publicstaticresources)中任意一个然后访问测试 http://localhost:8080/ 看结果
1、关于网站图标说明
欢迎页面(Welcome Page) Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application. 自定义应用图标Custom Facicon Spring Boot looks for a favicon.ico in the configured static content locations and the root of the classpath (in that order). If such a file is present, it is automatically used as the favicon of the application. 2、首页图标
2.2.x之前的版本如2.1.7springboot是这样
与其他静态资源一样Spring Boot在配置的静态内容位置中查找 favicon.ico。如果存在这样的文件它将自动用作应用程序的favicon。 关闭SpringBoot默认图标 #关闭默认图标
spring.mvc.favicon.enabledfalse自己放一个图标在静态资源目录下我放在 public 目录下 清除浏览器缓存Ctrl F5刷新网页发现图标已经变成自己的了
2.2.x之后的版本如2.3.0直接执行2和3就可以了
文章转载自: http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.rkqkb.cn.gov.cn.rkqkb.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.kjgdm.cn.gov.cn.kjgdm.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.rqlf.cn.gov.cn.rqlf.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.pbbzn.cn.gov.cn.pbbzn.cn http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn