欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot消息國(guó)際化配置實(shí)現(xiàn)過(guò)程解析

 更新時(shí)間:2020年07月31日 10:26:34   作者:與李  
這篇文章主要介紹了SpringBoot消息國(guó)際化配置實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、目的

針對(duì)不同地區(qū),設(shè)置不同的語(yǔ)言信息。

SpringBoot國(guó)際化配置文件默認(rèn)放在classpath:message.properties,如果自定義消息配置文件,需要application.properties或application.yml中設(shè)置spring.messages.basename的值。

二、步驟

在src/main/resources 下建i18n文件夾

在i18n文件夾中建立messages.properties 找不到語(yǔ)言配置時(shí),使用此文件

hello=你好_默認(rèn)

在i18n文件夾中建立messages_en_US.properties 英文語(yǔ)言配置

hello=hello_English

在i18n文件夾中建立messages_zh_CN.properties 中文語(yǔ)言配置

hello=你好_中文

MessageConfig.java

對(duì)消息的配置

package com.spring.security.config.spring;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.Assert;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AbstractLocaleContextResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class MessageConfig extends AbstractLocaleContextResolver{

	@Value("${spring.messages.basename}")
	public String[] basenames;

	@Bean(name = "messageSource")
	public ResourceBundleMessageSource resourceBundleMessageSource() {
		ResourceBundleMessageSource source = new ResourceBundleMessageSource();
		if (basenames != null) {
			for (int i = 0; i < basenames.length; i++) {
				String basename = basenames[i];
				Assert.hasText(basename, "Basename must not be empty");
				this.basenames[i] = basename.trim();
			}
			source.setBasenames(basenames);
		} else {
			this.basenames = new String[0];
			source.setBasename(basenames[0]);
		}
		source.setDefaultEncoding("UTF-8");
		source.setUseCodeAsDefaultMessage(true);
		return source;
	}

  @Bean
  public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
  }

  /**
   * 國(guó)際化,設(shè)置url識(shí)別參數(shù)
   *
   * @return
   */
  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
  }

	@Override
	public LocaleContext resolveLocaleContext(HttpServletRequest request) {
		return null;
	}

	@Override
	public void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
			LocaleContext localeContext) {
	}
}

SpringUtils.java

Spring工具類,用于獲取ApplicationContext

package com.spring.security.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * Spring容器
 */
@Service
public class SpringUtils implements ApplicationContextAware {

  private static ApplicationContext context = null;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (context == null) {
      context = applicationContext;
    }
  }

  /**
   * 獲取容器
   *
   * @return 容器
   */
  public static ApplicationContext getContext() {
    return context;
  }
}

MessageUtils.java

封裝獲取message的工具類

package com.spring.security.common.utils;

import java.util.Locale;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class MessageUtils {

	public static String getMessage(String code) {
		 Locale locale = LocaleContextHolder.getLocale();
		 ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
	   String message = reloadableResourceBundleMessageSource.getMessage(code, null, locale);
	   return message;
	}
}

** WebMvcConfig.java**

mvc配置,解決跨域,接口中文亂碼,添加語(yǔ)言攔截器

package com.spring.security.config.spring;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Autowired
	private LocaleChangeInterceptor localeChangeInterceptor;

	/**
	 * 解決跨域
	 */
	@Override
	protected void addCorsMappings(CorsRegistry registry) {
		registry
		.addMapping("/**")
		.allowedHeaders("*")
		.allowedMethods("*")
		.allowedOrigins("*")
		.allowCredentials(true);
	}

	/**
	 * 配置消息轉(zhuǎn)換器
	 * 解決返回String亂碼
	 */
	@Override
	protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		converters.add(responseBodyConverter());
	}

	@Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    return new StringHttpMessageConverter(Charset.forName("UTF-8"));
  }

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		registry.addInterceptor(localeChangeInterceptor);
	}

}

三、測(cè)試

測(cè)試接口:

package com.spring.security.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spring.security.common.utils.I18nUtils;

@RestController
public class TestController {

	@GetMapping("/test")
	public String doTest() {
		return I18nUtils.getMessage("hello");
	}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中ReentrantLock的用法和原理

    Java中ReentrantLock的用法和原理

    本文主要介紹了Java中ReentrantLock的用法和原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法

    SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法

    Spring?Boot是一個(gè)用于構(gòu)建Web應(yīng)用程序的框架,通過(guò)AOP可以實(shí)現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來(lái)防止表單重復(fù)提交的方法,需要的朋友可以參考下
    2023-04-04
  • Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)動(dòng)態(tài)切換示例

    Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)動(dòng)態(tài)切換示例

    本篇文章主要介紹了Spring配置多個(gè)數(shù)據(jù)源并實(shí)現(xiàn)動(dòng)態(tài)切換示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Spring框架花式創(chuàng)建Bean的n種方法(小結(jié))

    Spring框架花式創(chuàng)建Bean的n種方法(小結(jié))

    這篇文章主要介紹了Spring框架花式創(chuàng)建Bean的n種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • SpringBoot+RabbitMQ方式收發(fā)消息的實(shí)現(xiàn)示例

    SpringBoot+RabbitMQ方式收發(fā)消息的實(shí)現(xiàn)示例

    這篇文章主要介紹了SpringBoot+RabbitMQ方式收發(fā)消息的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java servlet手機(jī)app訪問(wèn)接口(三)高德地圖云存儲(chǔ)及檢索

    java servlet手機(jī)app訪問(wèn)接口(三)高德地圖云存儲(chǔ)及檢索

    這篇文章主要為大家詳細(xì)介紹了java servlet手機(jī)app訪問(wèn)接口(三),高德地圖云存儲(chǔ)及檢索,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Java多種方法實(shí)現(xiàn)合并多個(gè)list對(duì)象列表

    Java多種方法實(shí)現(xiàn)合并多個(gè)list對(duì)象列表

    Java編程中,合并多個(gè)列表對(duì)象可以通過(guò)Stream?API或傳統(tǒng)循環(huán)方式實(shí)現(xiàn),使用Stream?API合并時(shí),利用flatMap方法將嵌套的List展平,再通過(guò)collect方法收集成一個(gè)新的列表,傳統(tǒng)循環(huán)則通過(guò)創(chuàng)建一個(gè)空的ArrayList,并通過(guò)遍歷每個(gè)列表將元素添加進(jìn)去
    2024-09-09
  • 解析java中的error該不該捕獲

    解析java中的error該不該捕獲

    這篇文章主要介紹了java中的error該不該捕獲,需要的朋友可以參考下
    2014-02-02
  • 詳談Java中的Object、T(泛型)、?區(qū)別

    詳談Java中的Object、T(泛型)、?區(qū)別

    下面小編就為大家?guī)?lái)一篇詳談Java中的Object、T(泛型)、?區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Java和Scala集合間的相互轉(zhuǎn)換方式

    Java和Scala集合間的相互轉(zhuǎn)換方式

    這篇文章主要介紹了Java和Scala集合間的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論