Springmvc国际化自动配置代码实现
2020-04-11 07:05
519 查看
在 springmvc 中通过 LocalResover 获取区域信息对象 Local(对语言信息、国家代码等的封装),进行国际化配置,在 springboot 为我们提供了默认的区域信息解析器
@Configuration( proxyBeanMethods = false ) @ConditionalOnWebApplication( type = Type.SERVLET ) @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) @AutoConfigureOrder(-2147483638) @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class}) public class WebMvcAutoConfiguration { public static final String DEFAULT_PREFIX = ""; public static final String DEFAULT_SUFFIX = ""; private static final String[] SERVLET_LOCATIONS = new String[]{"/"}; @Bean @ConditionalOnMissingBean //容器中没有 LocaleResolver 时 @ConditionalOnProperty( prefix = "spring.mvc", name = {"locale"} ) public LocaleResolver localeResolver() { //如果属性中指定用固定的就用固定的 FixedLocaleResolver 区域化信息解析器 if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) { return new FixedLocaleResolver(this.mvcProperties.getLocale()); } else { //否则使用 AcceptHeaderLocaleResolver 区域化信息解析器 AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver(); localeResolver.setDefaultLocale(this.mvcProperties.getLocale()); return localeResolver; } }
AcceptHeaderLocaleResolver 在 request 请求头中获取到区域信息
public class AcceptHeaderLocaleResolver implements LocaleResolver { private final List<Locale> supportedLocales = new ArrayList(4); public Locale resolveLocale(HttpServletRequest request) { Locale defaultLocale = this.getDefaultLocale(); if (defaultLocale != null && request.getHeader("Accept-Language") == null) { return defaultLocale; } else { Locale requestLocale = request.getLocale(); List<Locale> supportedLocales = this.getSupportedLocales(); if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) { Locale supportedLocale = this.findSupportedLocale(request, supportedLocales); if (supportedLocale != null) { return supportedLocale; } else { return defaultLocale != null ? defaultLocale : requestLocale; } } else { return requestLocale; } } }
默认就是更具请求头带来的区域信息获取 Local 进行国际化
我们可以通过定义自己的区域化解析器生成区域化信息
public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { String l = request.getParameter("l"); Locale locale = Locale.getDefault(); if(!StringUtils.isEmpty(l)){ String[] split = l.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } } @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter { @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
量变引起质变,这就是我所理解的因果。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 配置Eclipse 实现按任意键代码自动补全
- Eclipse下配置Perl开发环境并实现代码自动补全
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- Jfinal 2.1版本,JFinalConfig里自动配置路由的代码实现,直接晒代码
- Webpack 2 视频教程 009 - 配置 ESLint 实现代码规范自动测试 (上)
- 这么详细的Spring boot Starter自动配置教程,不了解一下?(原理、demo代码实现以及面试题)...
- 配置Eclipse 实现按任意键代码自动补全
- SYD8801代码解析【BLE_SendData函数gap_att_report结构体设置】【蓝牙广播的实现】【协议栈回调事件】【自动睡眠】【使用内部晶振】【蓝牙状态机的说明】【断线时重新配置IO口】
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 配置Eclipse 实现按任意键代码自动补全
- 配置Eclipse 实现按任意键代码自动补全
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- window 下 svn 服务器配置 并实现代码自动部署
- 配置Eclipse 实现按任意键代码自动补全
- svn配置post-commit实现自动上传代码至测试服务器
- mac phpstorm 配置 deployment 实现代码自动上传
- Webpack 2 视频教程 010 - 配置 ESLint 实现代码规范自动测试 (下)
- springmvc,freemarker,spring.ftl实现国际化配置解决方案
- 自己很丰衣足食Geany中tidy配置,实现自动格式化代码功能