SpringBoot - 访问静态资源

本文介绍如何在SpringBoot中配置并访问静态资源,包括从classpath/static及servletContext下的webapp目录访问图片和HTML文件的方法。

1.从classpath/static访问

在resources下建立一个static的folder(必须是static名字的),放一张图片,用来做测试

 相当简单,执行这个main就OK了

package cn.bl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

访问:

http://localhost:8080/1.png

图片粗来,OK

然后改版一下,将图片放到imgs文件夹下,写一个index.html,访问这个html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>访问静态资源</title>
</head>
<body>
	静态资源:
	<img alt="photo" src="imgs/1.png">
</body>
</html>

 访问:http://localhost:8080/index.html

发现没有乱码,图片也粗来了(使用SpringBoot感觉很爽的一点是,它基本上都是基于UTF-8的)

2.访问servletContext下

在src/main下创建webapp文件夹(必须是这个名字的)

 index可以使用原理的index.html

然后访问http://localhost:8080/index2.html

图片和文字都粗来了,OK

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值