SpringBoot4-Web3-SpringMVC
扩展SpringMVC
1 |
|
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter,不标注@EnableWebMvc 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package com.wsx.springboothelloworld.config;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/wsx").setViewName("templates_hello");
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class WebMvcAutoConfiguration {
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = "";
private static final String[] SERVLET_LOCATIONS = { "/" };1
2
3
4
5
6
7
8
9
10
11// Defined as a nested config to ensure WebMvcConfigurer is not read when not
// on the classpath
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
private final ResourceProperties resourceProperties;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}1
2
3
4
5
6
7
8
9
10/**
* Configuration equivalent to {@code @EnableWebMvc}.
*/
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private final WebMvcProperties mvcProperties;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/*
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}1
2
3
4
5
6
public EnableWebMvc {
}1
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
1
2
3
4
5
6
7
8
9
public class WebMvcAutoConfiguration {
在springboot中有很多xxxConfiguier帮助我们扩展配置, ## 在骚一点 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//@EnableWebMvc
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/wsx").setViewName("templates_hello");
}
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
return new WebMvcConfigurerAdapter() {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/wsx2").setViewName("templates_hello");
registry.addViewController("/wsx3").setViewName("templates_hello");
}
};
}
}1
2
3
4
5
6
7<!-- bootstrap-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>