这里以首页的路径响应路径配置为例
@Controller注解的方法
@Controller
public class HelloWorldController {
@RequestMapping({"/","/index.html"})
public String index(){
return "index";
}
}
这个类要放在controller包下
重写配置类方法或者添加组件的方法
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
//浏览器发送请求到
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
//所有的WebMvcConfigurerAdapter组件都会一起作用
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter Adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
return Adapter;
}
}
1.这里重写MyMvcConfig(WebMvcConfigurerAdapter implements WebMvcConfigurer)这个配置类,的方法来扩展功能
2.同时也把webMvcConfigurerAdapter这个组件加入到容器中,组件里面也是重写了方法来扩展功能。
3.本质上这里有两个配置类,只不过以不同的方式加入到容器中,重写的都是同时一个方法