Springmvc国际化自动配置代码实现

网友投稿 254 2022-12-10


Springmvc国际化自动配置代码实现

在 springmvc 中通过 LocalResoverhttp:// 获取区域信息对象 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());

http:// return localeResolver;

}

}

AcceptHeaderLocaleResolver 在 request 请求头中获取到区域信息

public class AcceptHeaderLocaleResolver implements LocaleResolver {

private final List 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 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(HttpServletRequesKgXZCjxsZt 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();

}

}

量变引起质变,这就是我所理解的因果。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java JDBC基本使用方法详解
下一篇:Springboot错误处理机制实现原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~