springboot静态资源的位置

默认的位置

springboot静态资源的位置
静态资源是指用户可以直接通过浏览器输入url就能访问得到的资源。SpringBoot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

  • /static
  • /public
  • /resources
  • /META-INF/resources
    举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/logo.jpg。如能显示图片,配置成功。在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。页面放在了templates中,这个不同于之前maven创建javaweb的方式页面放在webapp下。对于freemarker来说,src/main/resources 下的templates文件夹下存放页面(ftl文件),注意,如果静态资源的路径和某个Controller方法的映射url相同,则会匹配Controller方法而不是静态资源,因此一般需要对静态资源的路径匹配做单独的处理
自定义配置

自定义配置路径

方式一
# 图片音频上传路径配置(win系统自行变更本地路径)
web.upload.path=/home/file1/
web.front-path=/home/file2/
# 静态文件请求匹配方式
spring.mvc.static-path-pattern=/upload/**
# 修改默认的静态寻址资源目录 多个使用逗号分隔
#spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/itstyle/
#file
spring.resources.static-locations=file:${web.upload-path},file:${web.front-path}

注意要以/结尾,file:表示的是file协议,即本地硬盘路径
访问路径就是scheme://ip:port/context-path/upload/**/xx.jpg

方式二
# 图片音频上传路径配置(win系统自行变更本地路径)
web.upload.path=/home/file/
@SpringBootApplication
public class WechatApplication extends WebMvcConfigurerAdapter  {
    
    private final static Logger LOGGER = LoggerFactory.getLogger(WechatApplication.class);
    
    @Value("${web.upload.path}")
    private String uploadPath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/uploads/**").addResourceLocations(
                "file:" + uploadPath);
        LOGGER.info("自定义静态资源目录、此处功能用于文件映射");
    }
    
    public static void main(String[] args) {
        SpringApplication.run(WechatApplication.class);
        LOGGER.info("语音测评后台服务启动成功");
    }
 
}
设置模板引擎

模板引擎
freemarker模板引擎默认的路径是src/main/resources/templates,文件后缀是ftl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值