1、静态资源
//第一种方法:
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
//第二种方法:
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
this.staticPathPattern = "/**";
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
优先级高到低是:resources,static(默认),public
在yaml自定义spring.mvc.static-path-pattern会使得以上路径失效
2、访问首页
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
return welcomePageHandlerMapping;
}
private Optional<Resource> getWelcomePage() {
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
静态资源下的index.html会被/**映射,所以访问http://localhost:8080/就可以访问到主页
3、图标
这个功能在新版springboot已经被移除,因为前端直接修改图标更快捷
在WebMvcAutoConfiguration自动配置类中可找到FaviconConfiguration方法即可使用
①首先在配置修改
#关闭默认图标
spring.mvc.favicon.enabled=false
②接着将favicon.ico图标文件放于静态文件中即可
注意:
版本更替快,有一些功能运用不了,可以去查看源码是否还存在该组件
4、Thymeleaf
ThymeleafProperies中所显示Thymeleaf所进行跳转的位置
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
页面得先加:
<html lang="en" xmlns:th="http://www.thymeleaf.org" x>
语法运用:
对于后端传过来的model绑定的数据的调用
<div th:text="${msg}"></div>
5、扩展自动配置类
当你要自定义一些定制的功能,只需要写好组件把其交给springboot配置类,就会帮我们自动装配。
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//实现了视图解析其接口的类,我们就可以把它看做视图解析器
@Bean
public ViewResolver myViewResolver(){
return new MyViewResolver();
}
//定义一个自己的视图解析器
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
第二种扩展(根据WebMvcConfigurer接口的方法进行重写,官方建议我们这样去扩展)
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/cool").setViewName("index");
}
}
小结:在springboot中有很多的XXXConfiguration帮助我们进行扩展配置,只要看见它,就得注意扩展了什么功能
6、Format
翻找源码可得配置为:spring.mvc.format.date=dd/MM/yyyy(该配置也与老师教学不同,因为版本问题的更替,所以查看源码进行配置的查看或者查看文档才能知道其运用的正确方法)