当前位置: 首页 > news >正文

算命公司网站建设制作开发方案无极网站设计

算命公司网站建设制作开发方案,无极网站设计,厦门市建设局电工报名网站,dnf免做卡网站一、前言 springboot配置静态资源方式是多种多样#xff0c;接下来我会介绍其中几种方式#xff0c;并解析一下其中的原理。 二、使用properties属性进行配置 应该说 spring.mvc.static-path-pattern 和 spring.resources.static-locations这两属性是成对使用的#xff0c;如…一、前言   springboot配置静态资源方式是多种多样接下来我会介绍其中几种方式并解析一下其中的原理。 二、使用properties属性进行配置   应该说 spring.mvc.static-path-pattern 和 spring.resources.static-locations这两属性是成对使用的如果不明白其中的原理总会出现资源404的情况。首先收一下spring.mvc.static-path-pattern代表的是一个Ant Path路径例如resources/**表示当你的路径中存在resources/**的时候才会处理请求。比如我们访问“http://localhost:8080/resources/xxx.js”时很显然springboot逻辑中会根据模式匹配对url进行匹配匹配命中后是如何再定位到具体的资源的呢这时候spring.resources.static-locations的配置就起作用了。   忘记说了在springboot中spring.mvc.static-path-pattern的默认值是/**spring.resources.static-locations的默认值是classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,servlet context:/springboot中相关的ResourceHttpRequestHandler就会去spring.resources.static-locations配置的所有路径中寻找资源文件。   所以我之前才说spring.mvc.static-path-pattern 和 spring.resources.static-locations这两属性是成对使用的。 三、springboot中默认对静态资源的处理   调试过程中通过查看 org.springframework.web.servlet.DispatcherServlet中的handlerMappings变量我们发现有一个很显眼的 resourceHandlerMapping 这个是springboot为我们提供的一个默认的静态资源handler通过全文搜索发现出现在org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport这个类中也就是这个类包含了EnableWebMvc注解中的大多数功能更多的扩展功能请参考org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration。   resourceHandlerMapping 的定义如下。 /*** Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped* resource handlers. To configure resource handling, override* {link #addResourceHandlers}.*/ Bean public HandlerMapping resourceHandlerMapping() {ResourceHandlerRegistry registry new ResourceHandlerRegistry(this.applicationContext,this.servletContext, mvcContentNegotiationManager());addResourceHandlers(registry);AbstractHandlerMapping handlerMapping registry.getHandlerMapping();if (handlerMapping ! null) {handlerMapping.setPathMatcher(mvcPathMatcher());handlerMapping.setUrlPathHelper(mvcUrlPathHelper());handlerMapping.setInterceptors(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));handlerMapping.setCorsConfigurations(getCorsConfigurations());}else {handlerMapping new EmptyHandlerMapping();}return handlerMapping; }   请大家先记住ResourceHandlerRegistry这个类。     首先看一下addResourceHandlers(registry);这个方法父类DelegatingWebMvcConfiguration做了实现如下。 private final WebMvcConfigurerComposite configurers new WebMvcConfigurerComposite(); Override protected void addResourceHandlers(ResourceHandlerRegistry registry) {this.configurers.addResourceHandlers(registry); }   其中WebMvcConfigurerComposite是操作了WebMvcConfigurer类型的对象的集合。在org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration这个springmvc的自动配置类中有一个WebMvcConfigurer的实现类如下。 // Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not // on the classpath Configuration Import(EnableWebMvcConfiguration.class) EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {...Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug(Default resource handling disabled);return;}Integer cachePeriod this.resourceProperties.getCachePeriod();if (!registry.hasMappingForPattern(/webjars/**)) {customizeResourceHandlerRegistration(registry.addResourceHandler(/webjars/**).addResourceLocations(classpath:/META-INF/resources/webjars/).setCachePeriod(cachePeriod));} String staticPathPattern this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));}}... }   上面的addResourceHandlers方法中增加了默认的mapping pattern  /webjars/** 默认的resource location是classpath:/META-INF/resources/webjars/。正是这里的配置我们在集成swagger的时候就可以正常访问到swagger webjars中的js文件了。其中红色的代码部分就是用户可以自定义的默认静态资源访问方式并通过ResourceHandlerRegistry对象进行注册。接着看一下mvcProperties和resourceProperties对应的类吧。 ConfigurationProperties(spring.mvc) public class WebMvcProperties {.../*** Path pattern used for static resources.*/private String staticPathPattern /**;... }   WebMvcProperties类中的staticPathPattern field 对应了spring.mvc.static-path-pattern这个属性可以看到默认值是 /**。 ConfigurationProperties(prefix spring.resources, ignoreUnknownFields false) public class ResourceProperties implements ResourceLoaderAware {.....private static final String[] SERVLET_RESOURCE_LOCATIONS { / };private static final String[] CLASSPATH_RESOURCE_LOCATIONS {classpath:/META-INF/resources/, classpath:/resources/,classpath:/static/, classpath:/public/ };private static final String[] RESOURCE_LOCATIONS;static {RESOURCE_LOCATIONS new String[CLASSPATH_RESOURCE_LOCATIONS.length SERVLET_RESOURCE_LOCATIONS.length];System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,SERVLET_RESOURCE_LOCATIONS.length);System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);}private String[] staticLocations RESOURCE_LOCATIONS;...... }   ResourceProperties中staticLocations field 对应了 spring.resources.static-locations 这个属性。可以看到默认值是classpath:[/META-INF/resources/, /resources/, /static/, /public/], servlet context:/ 四、静态资源的Bean配置   在了解了springboot默认资源的配置的原理即 spring.mvc.static-path-pattern 和 spring.resources.static-locations我们可以增加一个WebMvcConfigurer类型的bean来添加静态资源的访问方式还记得上面说的“请记住ResourceHandlerRegistry这个类“下面就用到了哦。 Configuration public class ResourceWebMvcConfigurer extends WebMvcConfigurerAdapter {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/resources/**).addResourceLocations(classpath:/public-resources/).setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());} }   那么当访问路径中包含resources/**的时候resource handler就会去classpath:/public-resources目录下寻找了。 五、静态资源的查找   参考 org.springframework.web.servlet.resource.ResourceHttpRequestHandlerResourceHttpRequestHandler中通过org.springframework.web.servlet.resource.PathResourceResolver进行查找。   举个例子下图是springboot打包之后的目录结构现在想要通过url访问application.properties文件springboot默认的静态文件配置可以吗当然需要用事实来说话了。        我们已经知道默认的resource locations中有个 servlet-context:/访问你的url是http://localhost:8080/工程名/application.properties调试一下PathResourceResolver结果如下。         发现servlet-context的根路径如上图所示查看一下这个路径对应的目录发现什么都没有所以很显然无法找到我们要找的文件了。毕竟一般使用springboot都是jar项目servlet-context path下没有用户自定义的资源。  六、其他方式   在Servlet3协议规范中包含在JAR文件/META-INFO/resources/路径下的资源可以直接访问了。如果将springboot项目打包成war包可以配置一个默认的servlet。在WebMvcConfigurationSupport中已经定义好了不过默认是一个EmptyHandlerMapping。 /*** Return a handler mapping ordered at Integer.MAX_VALUE with a mapped* default servlet handler. To configure default Servlet handling,* override {link #configureDefaultServletHandling}.*/ Bean public HandlerMapping defaultServletHandlerMapping() {DefaultServletHandlerConfigurer configurer new DefaultServletHandlerConfigurer(servletContext);configureDefaultServletHandling(configurer);AbstractHandlerMapping handlerMapping configurer.getHandlerMapping();handlerMapping handlerMapping ! null ? handlerMapping : new EmptyHandlerMapping();return handlerMapping; }   可以通过自定义一个WebMvcConfigurer类型的bean改写configureDefaultServletHandling 方法如下。 Configuration public class MyWebConfigurer extends WebMvcConfigurerAdapter {Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();} }   这样就设置了一个默认的servlet在加载静态资源的时候就会按照servelt方式去加载了。     就先分享这么多了更多分享请关注我们的技术公众号吧
http://www.yutouwan.com/news/364744/

相关文章:

  • 龙华做网站公司好的网站设计制作
  • 网站营销 优势wordpress显示不同的页脚
  • 烟台seo网站推广营销公司是什么意思
  • 跨境自建站模板oa软件
  • 专业做网站建设公司怎么样seo推广软件代理
  • 做asp.net网站参考文献电商网站开发源码
  • 人工智能 网站建设武昌做网站多少钱
  • 中国建设银行官方网站 认证wordpress woocommerce 插件
  • 铁路网站建设论文链接提交工具的推荐词
  • 做哪些网站好可以做点赞的网站赚钱
  • 模拟网站开发免费做字体的网站
  • 服装公司网站结构添加网站备案号链接
  • wordpress 数据库 nginxseo托管服务
  • 广告设计模板网站红色系 网站
  • 做网店去哪个网站货源好html可以做网站后台吗
  • 个人网站如何加入百度联盟网站添加新闻栏怎么做
  • 网页制作与网站建设的题陈铭生个人资料简介
  • 网站如何减少404跳转金融行业做网站
  • 医疗网站织梦wordpress的商城网站制作公司
  • 模板自助建站网站制作php企业网站demo
  • 徐州专业网站建设上海新闻坊
  • 湖南网站建设seoapp软件开发培训班
  • 网站百度排名查询网站的收费窗口怎么做
  • 建站网站的图片织梦网站首页自动更新
  • 重庆展示型网站制作厦门网站建设团队
  • 卧龙区微网站建设酷炫给公司网站欣赏
  • 可以做ppt的网站有哪些集团网站建设基础方案
  • 杭州企业网站建设公司自己怎么做网站免费的
  • 做导购类网站网站建设与运营 pdf
  • 房地产公司 网站建设网站教程dw