一、静态资源导入

用以存储html、js文件

WebMvcAutoConfiguration类中addResourceHandlers方法

1、webjars

  1. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  2. if (!this.resourceProperties.isAddMappings()) {
  3. logger.debug("Default resource handling disabled");
  4. } else {
  5. // webjars方式
  6. Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
  7. CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
  8. if (!registry.hasMappingForPattern("/webjars/**")) {
  9. this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
  10. }
  11. String staticPathPattern = this.mvcProperties.getStaticPathPattern();
  12. if (!registry.hasMappingForPattern(staticPathPattern)) {
  13. this.customizeResourceHandlerRegistration(
  14. registry.addResourceHandler(new String[]{staticPathPattern})
  15. .addResourceLocations(
  16. // 指定当前目录
  17. WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())
  18. )
  19. .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
  20. }
  21. }
  22. }

2、配置的常规路径

org.springframework.boot.autoconfigure.web.ResourceProperties类
可以直接访问。

  1. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
  2. "classpath:/META-INF/resources/",
  3. "classpath:/resources/",
  4. "classpath:/static/",
  5. "classpath:/public/"
  6. };

3、application.properties指定

  1. spring.mvc.static-path-pattern=

二、首页配置

必须配置index.html 文件,放在静态目录下

  1. @Bean
  2. public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
  3. WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
  4. welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
  5. welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
  6. return welcomePageHandlerMapping;
  7. }
  8. private Optional<Resource> getWelcomePage() {
  9. String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
  10. return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
  11. }
  12. private Resource getIndexHtml(String location) {
  13. return this.resourceLoader.getResource(location + "index.html");
  14. }

三、模板引擎

https://www.cnblogs.com/hellokuangshen/p/12516870.html
https://www.thymeleaf.org/
SpringBoot内嵌的tomcat不支持jsp,所以需要使用模板引擎,SpringBoot官方推荐使用thymeleaf。
数据 和 html进行组装
image.png
[

](https://www.thymeleaf.org/)