spring boot静态资源文件的访问以及自定义

来源:互联网 发布:蚂蚁金服 基金知乎 编辑:程序博客网 时间:2024/06/07 05:48

上一节出现的页面是不是很丑?我才不告诉你,我把背景都处理了偷笑,

相关的项目结构,如下


好吧.来看看最终的页面源码

<!DOCTYPE HTML><html><head>    <#include "/common/common.ftl" />    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    <meta name="description" content="药帮忙网上商城是武汉小药药医药科技有限公司旗下国家药监局批准的正规药品采购、批发、销售网上药品交易电子商务平台,主要经营中西成药、营养保健、医疗器械、全部针剂等医药品类。药帮忙是全国正规合法网上采购药品平台,以全新的互联网营销理念,满足客户多方面需求。以诚信服务于广大用户,优化医药供应链流程为经营理念。原供货源直销医药商品,质量保障,同品质药品价格比市场更优惠。 ">    <meta name="keywords" content="网上药店, 网上买药,网上购药,网上药品交易平台,网上药品批发,药品网站,药品批发,药品,药品网,医药批发,医药批发市场, 药品交易">    <title>会员登录 - 通行证中心 - 药帮忙</title>    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">    <meta name="renderer" content="webkit">    <link rel="stylesheet" href="${ctx}/css/login.css?t=20170718"/><script type="text/javascript" src="${ctx}/js/login.js?t=20170718"></script>    <script src="${ctx}/js/plugins/jquery.md5.js" type="text/javascript"></script></head><body><div class="container">    <!--主体部分开始-->    <div class="main">        <div class="loginmain">            <div class="loginbox">                <ul class="l-title">                    <li class="phonelogin cur phcolor">账户登录</li>                </ul>                <div>    <#if tempFavorite??>    ${tempFavorite.id }<br/>    ${tempFavorite.productId }<br/>    ${tempFavorite.merchantId }<br/>    ${tempFavorite.status }<br/>    ${tempFavorite.creator }<br/>    ${tempFavorite.createTime?string('yyyy-MM-dd HH:mm:ss') }    </#if>    </div>        </div>    </div></div></div><script></script></body></html>

spring boot默认的静态资源文件配置可以详见ResourceProperties.java

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

默认的静态资源文件夹:为static/public,遵循spring boot默认规则,基本可以满足我们大部分的需求了,点击main主入口访问,看看书不是漂亮多了?

如果我们需要自定义,这里提供两种方法:

1:application.properties方法

这里的static可以换成你对应的文件夹名字

#静态资源文件处理spring.mvc.static-path-pattern=/static/**spring.resources.static-locations=classpath:/static/

如:myStatic

spring.mvc.static-path-pattern=/myStatic/**spring.resources.static-locations=classpath:/myStatic/
对应路径如下,


访问路径:

http://localhost:8081/myStatic/css/common.css

当然,你比较任性(其实我也比较任性,研究这个花了3天),我喜欢原来spring web项目的格局,不要最上层目录.裸奔静态资源

spring.mvc.static-path-pattern=/luoben/**spring.resources.static-locations=classpath:/
目录结构如下:


访问路径:

http://localhost:8081/luoben/css/common.css


2:重写 WebMvcConfigurerAdapter配置

package com.xyy.util;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class MyWebAppConfig extends WebMvcConfigurerAdapter {@Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");        super.addResourceHandlers(registry);    }}
上面具体的个性配置.也一样,修改对应的ResourceHand;ers与ResourceLocations即可