Spring boot 扩展 视图解析器
在spring boot 中对 视图解析器 进行扩展
1、实现 WebMvcConfigurer 接口
2、定义一个内部类实现 视图解析器 接口
3、将其交给springboot管理,会为我们自动装配
代码如下:
package springbootstudy.helloworld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
@Configuration
public class MyConfig implements WebMvcConfigurer {
//将其交给springboot管理,会为我们自动装配
@Bean
public ViewResolver myViewResolver(){
return new MyViewResolver();
}
//只要实现了ViewResolver接口,就可以把它看做一个视图解析器
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
4、在 DispatcherServlet 类上的 doDispatch 方法上打一个断点进行调试
可以看到,在所有的视图解析器集合中,已经存在自定义的视图解析器。