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

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

相关的项目结构,如下


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

<!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;

@Configuration
public class MyWebAppConfig extends WebMvcConfigurerAdapter {
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
}
上面具体的个性配置.也一样,修改对应的ResourceHand;ers与ResourceLocations即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一屁小肥咩

您的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值