**
SpringBoot的SpringMvc扩展配置(自定义视图解析器、转换器、格式化器、消息转换器、拦截器等)
**
扩展Spring mvc的配置,支持如下扩展配置:*
1、视图解析器:ContentNegotiatingViewResolver代理,BeanNameViewResolver的扩展,配置自定义视图解析器
2、自定义Converter、GenericConverter转换器和Formatter格式化器
3、自定义HttpMessageConverter消息转换器
4、自定义指定首页
5、自定义页面图片favicon
6、web的一些初始化指定(ConfigurableWebBindingInitilizer)
实现Spring Mvc的扩展配置:
创建一个类,使用@Configuration修饰,并实现WebMvcConfigurer接口,且该类不能使用@EnableWebMvc修饰(该注解导入了DelegatingWebMvcConfiguration类,该类会从容器中获取所有的webMvcConfig,但自动配置类WebMvcAutoCOnfiguration必须在某些类不存在的时候才生效,该注解获取的类中包含了这些类,将会导致WebMvcConfiguration失效)
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//添加视图控制,视图跳转,可用于定制首页
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//当客户端访问/路径时,返回逻辑视图名index给视图解析器处理
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main").setViewName("dashboard");
}
//自定义的国际化组件,配置成容器的Bean
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
@Override
//添加自定义拦截器
public void addInterceptors(InterceptorRegistry registry) {
//添加拦截器
registry.addInterceptor(new LoginHandlerInterceptor())
//添加拦截的路径
.addPathPatterns("/**")
//添加排除的路径(即放行的路径)
.excludePathPatterns("/index.html","/","/user/login"
,"/css/*","/js/*","/img/*");
}
}
自定义拦截器:
1、定义一个类实现HandlerInterceptor接口,打开该接口,复制重写三个方法
public class LoginHandlerInterceptor implements HandlerInterceptor {
//在控制器方法处理请求之前调用,返回值决定请求是否被继续处理
// 返回值为false时,不会调用控制器方法继续处理请求,为true会继续处理该请求
//该方法可以对请求参数、请求属性进行预处理
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser == null){
request.setAttribute("tip","没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
return true;
}
//控制器方法处理完请求之后,生成视图响应之前调用,可以通过ModelAndView参数添加额外的model属性,甚至修改逻辑视图名
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
//整个请求处理结束,响应也结束之后调用,主要用于资源清理
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
2、到Spring Mvc扩展配置类(实现了在这里插入代码片
WebMvcConfigurer且使用了@Configuration修饰的类)中添加该拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加拦截器
registry.addInterceptor(new LoginHandlerInterceptor())
//添加拦截的路径
.addPathPatterns("/**")
//添加排除的路径(即放行的路径)
.excludePathPatterns("/index.html","/","/user/login"
,"/css/*","/js/*","/img/*");
}
默认的国际化:
默认国际化使用AcceptHeaderLocalResolver实现类解析Locale,该解析器跟会根据客户端请求头Accept-Language判断客户端Locale,如:以zh-CN开头,则解析为中文。
spring mvc需要使用ResourceBundlerMessageSource加载国际化资源文件,需要显示去配置,SpringBoot已经自动配置了messageSourceAutoConfiguration管理国际化资源文件,因此不需要显示配置,如下:
实现自定义国际化:
1、编写国际化资源文件,文件名称:文件名_语言代码_国家代码,如:
message_zh_CN、message_en_US,内容为:key=value
2、自定义Locale解析器类,编写一个类,实现LocaleResolver接口,并实现该接口的方法
public class MyLocaleResolver implements LocaleResolver {
//解析请求
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
//获取请求参数
String language = httpServletRequest.getParameter("loc");
Locale locale = Locale.getDefault();//如果没有就使用默认的
//如果请求的链接携带了国际化参数
if(!StringUtils.isEmpty(language)){
String[] s = language.split("_");
locale = new Locale(s[0], s[1]);
return locale;
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
3、将该类配置为容器中的Bean
//自定义的国际化组件
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
4、在全局配置文件中设置加载的国家化资源文件名:
spring:
messages:i18n.login
5、在页面中使用thymeleaf的 #{}输出国际化消息