国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot消息國際化配置實現(xiàn)過程解析

瀏覽:4日期:2023-05-04 13:20:22

一、目的

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

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

二、步驟

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

在i18n文件夾中建立messages.properties 找不到語言配置時,使用此文件

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

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

hello=hello_English

在i18n文件夾中建立messages_zh_CN.properties 中文語言配置

hello=你好_中文

MessageConfig.java

對消息的配置

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;@Configurationpublic 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; } /** * 國際化,設(shè)置url識別參數(shù) * * @return */ @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName('lang'); return lci; }@Overridepublic LocaleContext resolveLocaleContext(HttpServletRequest request) {return null;}@Overridepublic 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容器 */@Servicepublic 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配置,解決跨域,接口中文亂碼,添加語言攔截器

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;@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport {@Autowiredprivate LocaleChangeInterceptor localeChangeInterceptor;/** * 解決跨域 */@Overrideprotected void addCorsMappings(CorsRegistry registry) {registry.addMapping('/**').allowedHeaders('*').allowedMethods('*').allowedOrigins('*').allowCredentials(true);}/** * 配置消息轉(zhuǎn)換器 * 解決返回String亂碼 */@Overrideprotected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {super.configureMessageConverters(converters);converters.add(responseBodyConverter());}@Bean public HttpMessageConverter<String> responseBodyConverter() { return new StringHttpMessageConverter(Charset.forName('UTF-8')); }@Overrideprotected void addInterceptors(InterceptorRegistry registry) {super.addInterceptors(registry);registry.addInterceptor(localeChangeInterceptor);}}

三、測試

測試接口:

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;@RestControllerpublic class TestController {@GetMapping('/test')public String doTest() {return I18nUtils.getMessage('hello');}}

SpringBoot消息國際化配置實現(xiàn)過程解析

SpringBoot消息國際化配置實現(xiàn)過程解析

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 中国黄色一级大片 | 欧美日韩精品国产一区在线 | 亚洲在线视频网站 | 91久久香蕉 | 美国一级毛片免费 | 亚洲欧洲日产国产 最新 | 日韩欧美一区二区三区不卡在线 | 国产精品看片 | 亚洲美女在线观看 | 在线视频欧美日韩 | 欧美成人在线网站 | 天干天干天啪啪夜爽爽色 | 久久福利资源站免费观看i 久久高清精品 | 免费a级毛片网站 | 久久国产一区二区三区 | 久久网在线 | 青草青99久久99九九99九九九 | 免费国产一级 | 久久亚洲网 | 国产日韩久久久精品影院首页 | 韩国特级毛片 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片免费看 | 亚洲国产精品久久久久秋霞不卡 | 美国免费三片在线观看 | 久久成人国产精品免费 | 亚洲精品美女视频 | 特级a做爰全过程片 | 天天躁日日躁狠狠躁一级毛片 | 日韩欧美在线视频一区二区 | 亚洲情a成黄在线观看 | 久久频这里精品99香蕉久网址 | 最新福利片v国产片 | 亚洲精品99久久久久中文字幕 | 最近中文字幕精彩视频 | 精品国产区 | 亚洲欧美高清 | 久久国产精品久久久 | 久久99亚洲精品久久 | 99久久久免费精品免费 | 亚洲va在线va天堂va四虎 | 青青热在线精品视频免费 |