真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Springmvc如何實(shí)現(xiàn)國(guó)際化自動(dòng)配置

這篇文章主要講解了Springmvc如何實(shí)現(xiàn)國(guó)際化自動(dòng)配置,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

在 springmvc 中通過(guò) LocalResover 獲取區(qū)域信息對(duì)象 Local(對(duì)語(yǔ)言信息、國(guó)家代碼等的封裝),進(jìn)行國(guó)際化配置,在 springboot 為我們提供了默認(rèn)的區(qū)域信息解析器

@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  //容器中沒(méi)有 LocaleResolver 時(shí)
    @ConditionalOnProperty(
      prefix = "spring.mvc",
      name = {"locale"}
    )
    public LocaleResolver localeResolver() {        //如果屬性中指定用固定的就用固定的 FixedLocaleResolver 區(qū)域化信息解析器
      if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
        return new FixedLocaleResolver(this.mvcProperties.getLocale());
      } else {          //否則使用 AcceptHeaderLocaleResolver 區(qū)域化信息解析器
        AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
        localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
        return localeResolver;
      }
    }

AcceptHeaderLocaleResolver 在 request 請(qǐng)求頭中獲取到區(qū)域信息

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;
      }
    }
  }

默認(rèn)就是更具請(qǐng)求頭帶來(lái)的區(qū)域信息獲取 Local 進(jìn)行國(guó)際化

我們可以通過(guò)定義自己的區(qū)域化解析器生成區(qū)域化信息

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();
  }
}

量變引起質(zhì)變,這就是我所理解的因果。

看完上述內(nèi)容,是不是對(duì)Springmvc如何實(shí)現(xiàn)國(guó)際化自動(dòng)配置有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)頁(yè)名稱:Springmvc如何實(shí)現(xiàn)國(guó)際化自動(dòng)配置
本文路徑:http://weahome.cn/article/jssjde.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部