package com.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//- @Configuration //- 为了不覆盖application.properties配置,这里先注释掉,不让自定义视图解析器起作用
@EnableWebMvc
/**
* 自定义视图解析器
*
* springboot会从工程根目录resources下,
* 寻找META-INF/resources,public,resources,static这四个文件夹作为根路径"/",
* 这几个文件夹下的直属资源在页面中可以通过 "/xx.js" 加载到
*
* 1. 自定义视图解析器时只会在默认路径下添加子路径而不是替换
* 2. 需要注意的是直接放在工程根目录resources下的静态资源是加载不到的 !!!
*/
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//- 配置前缀
resolver.setPrefix("/WEB-INF/es/");
//- 配置后缀
resolver.setSuffix(".jsp");
return resolver;
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//- 添加后,该路径下的静态资源通过 "/myResource/xx.js" 访问
registry.addResourceHandler("/myResource/**").addResourceLocations("classpath:/myResource/");
super.addResourceHandlers(registry);
}
}