WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被废弃 。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类,具体实现可看这篇文章。https://www.jb51.net/article/174766.htm
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.web.servlet.config.annotation;
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
public interface WebMvcConfigurer {
void configurePathMatch(PathMatchConfigurer var1);
void configureContentNegotiation(ContentNegotiationConfigurer var1);
void configureAsyncSupport(AsyncSupportConfigurer var1);
void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1);
void addFormatters(FormatterRegistry var1);
void addInterceptors(InterceptorRegistry var1);
void addResourceHandlers(ResourceHandlerRegistry var1);
void addCorsMappings(CorsRegistry var1);
void addViewControllers(ViewControllerRegistry var1);
void configureViewResolvers(ViewResolverRegistry var1);
void addArgumentResolvers(List var1);
void addReturnValueHandlers(List var1);
void configureMessageConverters(List> var1);
void extendMessageConverters(List> var1);
void configureHandlerExceptionResolvers(List var1);
void extendHandlerExceptionResolvers(List var1);
Validator getValidator();
MessageCodesResolver getMessageCodesResolver();
}
接下来我们着重找几个方法讲解一下:
void addInterceptors(InterceptorRegistry var1); void addViewControllers(ViewControllerRegistry registry); void addResourceHandlers(ResourceHandlerRegistry registry); void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); void configureViewResolvers(ViewResolverRegistry registry); void configureContentNegotiation(ContentNegotiationConfigurer configurer);
1、addInterceptors(InterceptorRegistry registry)
此方法用来专门注册一个Interceptor,如HandlerInterceptorAdapter
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
}
值的指出的是,在这里重写addViewControllers方法,并不会覆盖WebMvcAutoConfiguration中的addViewControllers(在此方法中,Spring Boot将“/”映射至index.html),这也就意味着我们自己的配置和Spring Boot的自动配置同时有效,这也是我们推荐添加自己的MVC配置的方式。
3. 自定义资源映射addResourceHandlers
比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。
注:如果继承WebMvcConfigurationSupport类实现配置时必须要重写该方法,具体见其它文章
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/my
public void enableContentNegotiation(View... defaultViews) {
initContentNegotiatingViewResolver(defaultViews);
}
该方法会创建一个内容裁决解析器ContentNegotiatingViewResolver ,该解析器不进行具体视图的解析,而是管理你注册的所有视图解析器,所有的视图会先经过它进行解析,然后由它来决定具体使用哪个解析器进行解析。具体的映射规则是根据请求的media types来决定的。
2). UrlbasedViewResolverRegistration()
public UrlbasedViewResolverRegistration jsp(String prefix, String suffix) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(prefix);
resolver.setSuffix(suffix);
this.viewResolvers.add(resolver);
return new UrlbasedViewResolverRegistration(resolver);
}
该方法会注册一个内部资源视图解析器InternalResourceViewResolver 显然访问的所有jsp都是它进行解析的。该方法参数用来指定路径的前缀和文件后缀,如:
registry.jsp("/WEB-INF/jsp/", ".jsp");
对于以上配置,假如返回的视图名称是example,它会返回/WEB-INF/jsp/example.jsp给前端,找不到则报404。
3). beanName()
public void beanName() {
BeanNameViewResolver resolver = new BeanNameViewResolver();
this.viewResolvers.add(resolver);
}
该方法会注册一个BeanNameViewResolver 视图解析器,这个解析器是干嘛的呢?它主要是将视图名称解析成对应的bean。什么意思呢?假如返回的视图名称是example,它会到spring容器中找有没有一个叫example的bean,并且这个bean是View.class类型的?如果有,返回这个bean。
4). viewResolver()
public void viewResolver(ViewResolver viewResolver) {
if (viewResolver instanceof ContentNegotiatingViewResolver) {
throw new BeanInitializationException(
"addViewResolver cannot be used to configure a ContentNegotiatingViewResolver. Please use the method enableContentNegotiation instead.");
}
this.viewResolvers.add(viewResolver);
}
这个方法想必看名字就知道了,它就是用来注册各种各样的视图解析器的,包括自己定义的。
6. configureContentNegotiation(ContentNegotiationConfigurer configurer)
上面我们讲了configureViewResolvers 方法,假如在该方法中我们启用了内容裁决解析器,那么configureContentNegotiation(ContentNegotiationConfigurer configurer) 这个方法是专门用来配置内容裁决的一些参数的。这个比较简单,我们直接通过一个例子看:
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true)
.ignoreAcceptHeader(true)
.parameterName("mediaType")
.defaultContentType(MediaType.TEXT_HTML)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
到这里我们就可以举个例子来进一步熟悉下我们上面讲的知识了,假如我们MVC的配置如下:
@EnableWebMvc
@Configuration
public class MyWebMvcConfigurerAdapte extends WebMvcConfigurerAdapter {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp");
registry.enableContentNegotiation(new MappingJackson2JsonView());
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true)
.ignoreAcceptHeader(true)
.parameterName("mediaType")
.defaultContentType(MediaType.TEXT_HTML)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
}
controller的代码如下:
@Controller
public class ExampleController {
@RequestMapping("/test")
public ModelAndView test() {
Map map = new HashMap();
map.put("哈哈", "哈哈哈哈");
map.put("呵呵", "呵呵呵呵");
return new ModelAndView("test", map);
}
}
在WEB-INF/jsp目录下创建一个test.jsp文件,内容随意。现在启动tomcat,在浏览器输入以下链接:http://localhost:8080/test.json,浏览器内容返回如下:
{
"哈哈":"哈哈哈哈",
"呵呵":"呵呵呵呵"
}
在浏览器输入http://localhost:8080/test 或者http://localhost:8080/test.html,内容返回如下:
this is test.jsp
显然,两次使用了不同的视图解析器,那么底层到底发生了什么?在配置里我们注册了两个视图解析器:
ContentNegotiatingViewResolver 和 InternalResourceViewResolver,还有一个默认视图:MappingJackson2JsonView。controller执行完毕之后返回一个ModelAndView,其中视图的名称为example1。
1.返回首先会交给ContentNegotiatingViewResolver 进行视图解析处理,而ContentNegotiatingViewResolver 会先把视图名example1交给它持有的所有ViewResolver尝试进行解析(本实例中只有InternalResourceViewResolver),
2.根据请求的mediaType,再将example1.mediaType(这里是example1.json 和example1.html)作为视图名让所有视图解析器解析一遍,两步解析完毕之后会获得一堆候选的List
3.根据请求的media type从候选的List
现在就可以理解上例中为何请求链接加上.json 和不.json 结果会不一样。当加上.json 时,表示请求的media type 为MediaType.APPLICATION_JSON,而InternalResourceViewResolver 解析出来的视图的ContentType与其不符,而与MappingJackson2JsonView 的ContentType相符,所以选择了MappingJackson2JsonView 作为视图返回。当不加.json 请求时,默认的media type 为MediaType.TEXT_HTML,所以就使用了InternalResourceViewResolver解析出来的视图作为返回值了。我想看到这里你已经大致可以自定义视图了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



