静态资源默认放在 resouces/static,路径为根路径,如 /xxx.js。如果希望把静态资源放到其他目录,可以实现接口 WebMvcConfigurer,在 registry 中注册静态资源的 URI 路径和存放的文件夹:

package com.xtuer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static-m/**").addResourceLocations("classpath:/page-m/static-m/");
        registry.addResourceHandler("/page-m/**").addResourceLocations("classpath:/page-m/");
    }
}

或者如下使用 @Bean 注解创建 WebMvcConfigurer 配置对象,这种方式的好处是可以在一个类里创建多个 @Bean:

package com.xtuer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static-m/**").addResourceLocations("classpath:/page-m/static-m/");
                registry.addResourceHandler("/page-m/**").addResourceLocations("classpath:/page-m/");
            }
        };
    }
}

results matching ""

    No results matching ""