SpringBoot之静态文件配置-yellowcong

SpringBoot配置静态文件跟原来的jsp略有不同啊,静态文件都放在了类路径的地址上,以前都是放到webapp下面,在配置文件中可以通过spring.mvc.static-path-pattern的方式来指定。第二种,是通过代码注册WebMvcConfigurerAdapter,来配置静态文件。

配置文件配置的方式(推荐)

1、配置静态文件地址

#访问请求的前缀
spring.mvc.static-path-pattern: /static/**
#静态文件地址(系统路径),file:D:/static
#类路径的文件地址 (这个默认是类路径,所以一般不用设定)
spring.resources.static-locations=classpath:/static/

下面是完整配置

logging:
  #日志存储地址
  file: "logs/config/demo-xx.log"
info:
  name : "入门案例"

server:
  #端口号
  #http://yellowcong.com:8888/
  port: 8888
  #项目名,如果不设定,默认是 /
  context-path: /
---
#spring.mvc.resources
spring:
  mvc:
    #配置静态文件地址
    static-path-pattern: /static/**
    view:
      # 页面默认前缀目录
      prefix: /WEB-INF/jsp/
      # 响应页面默认后缀
      suffix: .jsp

2、配置静态文件

静态文件夹目录,在src/main/resources目录下面。
这里写图片描述

3、访问服务

http://yellowcong.com:8888/static/js/jquery-1.7.1.js

这里写图片描述

代码指定的方式

启动类注册静态文件适配器

通过代码的这种方式,来搞起来比较麻烦,还不好找,所以一般建议使用使配置文件的方式

    @Component
    public class MyResHandler extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            super.addResourceHandlers(registry);
        }
    }

完整代码

package com.yellowcong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
public class ConfigMain {

    /**
     * @author yellowcong
     * 创建日期:2018/02/05
     * 设定首页
     */
    @Configuration
    public class DefaultView extends WebMvcConfigurerAdapter {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            //设定首页为index
            registry.addViewController("/").setViewName("forward:/index");

            //设定匹配的优先级
            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

            //添加视图控制类
            super.addViewControllers(registry);
        }
    }

    @Component
    public class MyResHandler extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            super.addResourceHandlers(registry);
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(ConfigMain.class, args);
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值