自定义一个自己的解析器然后注册到Bean里面
package com.luo.config;
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
/**
* @author LunarYouI
* @create 2021-03-22 16:01
*/
//如果你想自定义一些定制的功能,只要写这个组件,然后将它交给springboot,springboot就会自动装配
//扩展SpringMVC dispatchservlet
@Configuration //使当前这个类变成配置类
//一但表注了@EnableWebMvc,那么MVC将被全面接管
public class MyMvcConfig implements WebMvcConfigurer {
//public interface ViewResolver 实现了视图解析器接口的类,我们就可以把它看做视图解析器
/*第二步:将自己写的视图解析器注册到Bean里面,它就会自动帮我们装配上*/
@Bean
public ViewResolver MyViewResolver() {
return new MyViewResolver();
}
/*第一步:自定义了一个自己的视图解析器MyViewResolver*/
public static class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
如何看自己的视图解析器是否已经配置成功呢?使用Debug断点测试
1、使用shift+shift(不知道你们的快捷键是啥),查找到dispatcherServlet
2、然后点进去
3、ctrl+f 查找doDispatch进行断点
4、浏览器输入http://localhost:8080/
5、this下的viewResolvers就能找到我们自己写的哪个了