1. 程式人生 > >Spring Boot 國際化配置,訊息化引數

Spring Boot 國際化配置,訊息化引數

spirn boot 國際化配置,訊息化引數

1. 新增資原始檔


messages_zh_CN.properties

# 註冊登入
userid.is.null=                                 請輸入使用者名稱!
pwd.is.null=                                    請輸入密碼!
userid.error=                                   您輸入的使用者名稱格式不正確!
pwd.error=                                      您輸入的密碼格式不正確!
active.user
.is.null= 您的使用者名稱或密碼輸入錯誤! # 使用者管理 id.is.null= id不能為空! addr.is.null= 地址不能為空! sysuser.is.null= 使用者不存在! userstate.is.null= 請輸入使用者狀態! userstate.error
= 使用者狀態輸入錯誤,請輸入{0}或{1}!

messages_en_US.properties

userid.is.null=                                 please enter user name!
pwd.is.null=                                    Please enter your password!
userid.error=                                   The user name you entered is
not in the correct format! pwd.error= The password you entered is not in the correct format! active.user.is.null= Your username or password was entered incorrectly! # 使用者管理 id.is.null= Id cannot be empty! addr.is.null= The address cannot be empty! sysuser.is.null= User does not exist! userstate.error= User status entry error, please enter {0} or {1}!

2. 新增配置類

@Configuration
public class InitialConfig {

    /** 國際化檔案路徑 */
    @Value("${spring.messages.basename}")
    public String basename;

    /**
     * 用於解析訊息的策略介面,支援這些訊息的引數化和國際化。
     * 
     * @return
     */
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename(basename);
        return messageSource;
    }

}

3.校驗類中使用

/**
 * 校驗工具類
 *
 * @author colg
 */
@Component
public class CheckUtil {

    private static MessageSource source;

    /**
     * 在Spring裡,靜態變數/類變數不是物件的屬性,而是一個類的屬性,不能用@Autowired一個靜態變數(物件),使之成為一個SpringBean。<br />
     * 
     * 只能通過setter方法注入,並把類註解成為元件
     * 
     * 
     * @param source
     */
    @Autowired
    public void init(MessageSource source) {
        CheckUtil.source = source;
    }

    /**
     * 丟擲校驗錯誤異常
     * 
     * @param msgKey
     * @param args
     */
    private static void fail(String msgKey, Object... args) {
        // 訊息的引數化和國際化配置
        Locale locale = LocaleContextHolder.getLocale();
        msgKey = source.getMessage(msgKey, args, locale);
        throw new CheckException(msgKey);
    }
}

4. 展示效果


相關推薦

Spring Boot 國際化配置訊息引數

spirn boot 國際化配置,訊息化引數 1. 新增資原始檔 messages_zh_CN.properties # 註冊登入 userid.is.null= 請輸入使用者名稱!

spring eureka叢集+spring boot 微服務容器部署示例

1.搭建eureka docker叢集 (1)建立eureka容器 參考如下指令,在不同的伺服器上建立eureka容器。(提前在docker hup 上下載好java:8的映象) docker run -d --name registry1 -p 8762:808

Spring載入完畢時初始引數

三種方式:1、實現org.springframework.beans.factory.InitializingBean介面。 該介面實現其afterPropertiesSet方法,InitializingBean可以注入相關的service,如果在Spirng處理Init

Spring Boot配置檔案@ConfigurationProperties讀取List、Map引數

List application.properties custom.config.config1.folders[0]=/root custom.config.config1.folders[1]=/home/user1 custom.config.config1.folder

曹工談Spring BootSpring boot中怎麼進行外部配置一不留神摔一跤;一路debug原來是我太年輕了

# spring boot中怎麼進行外部化配置,一不留神摔一跤;一路debug,原來是我太年輕了 # 背景 我們公司這邊,目前都是spring boot專案,沒有引入spring cloud config,也就是說,配置檔案,還是放在resources下面的,為了區分多環境,是採用了profile這種方式

Spring Boot配置定時任務實現多線程操作

pre log pri http code china 部分 多線程操作 .net 參考的代碼部分 https://git.oschina.net/jokerForTao/spring_boot_schedule 一目了然!Spring Boot 中配置定時任務,實現

spring boot整合dubboSpring boot +Dubbo,簡易的配置方式

        剛做完一個基於motan的專案不久,便去看看dubbo的新特性了,dubbo自上年9月恢復更新到現在大概半年多,發現已經有和spring boot整合的配置了。個人喜歡的配置方式優先順序一般都是資原始

Spring boot 梳理 - 配置eclipse整合maven並開發Spring boot hello

@RestController @EnableAutoConfiguration public class App { @RequestMapping("/hello") public HashMap<String,String> hello(

Spring Boot 國際化(MessageSource)- 路徑配置之坑

腦洞大開,想著把專案老框架換成Spring Boot,在使用MessageSource進行國際化改造時遇神坑了(始終報:org.springframework.context.NoSuchMessageException: No message found un

spring boot 專案打包配置構建外掛

Spring Boot:jar中沒有主清單屬性 使用Spring Boot微服務搭建框架,在eclipse和Idea下能正常執行,但是在打成jar包部署或者直接使用java -jar命令的時候,提示了xxxxxx.jar中沒有主清單屬性: D:\hu-git\spr

搭建 spring boot (二) ---- 配置fastjson實體欄位格式化、欄位過濾、解決中文亂碼

新增第一個實體並返回json到前端 1、新增第一個實體類Demo.java,設定簡單的屬性id和name並生成getter和setter方法 2、新增介面。在ctroller中新增返回demo的介面 3、重啟應用並訪問介面。可見返回的資料已經是json格式了

Spring Boot自動配置(Auto-Configuration)@EnableAutoConfigurationSpring Beans和依賴注入

自動配置(Auto-Configuration) 自動配置(auto-configuration)是Spring Boot最重要的特性之一,因為該特性會根據應用的classpath(這個主要是根據maven pom.xml決定),annotations和其他的

spring boot配置網頁語言國際化

專案地址:https://gitee.com/indexman/spring_boot_in_action   開發步驟 1.編寫國際化配置檔案 場景是給登入頁面 login.html新增國際化支援。   2.使用ResourceBundleMe

[總結] Idea兩種方式設定spring boot應用配置引數

如下圖所示,在Idean的Edit configurations中編輯應用,第一種,配置VM options的引數時要以:-DparamName的格式設定引數。 第二種,配置程式引數,program arguments,使用 --paramName 的格式,也能設定程式引數

Spring-boot整合Quartz3、動態配置定時任務

通過動態新增JobDetail例項到Scheduler中,實現可配置動態式的定時任務作業 首先實現Job介面的 Myjob,通過包全名(com.example.demo.job.MyJob1),利用反射得到Clazz。 設定JobDetail的名字和組

spring boot filter 配置多個時執行順序

在 spring boot 配置Filter過濾器 中簡單介紹了spring boot 中如何新增過濾器,有人問到如果配置多個怎麼控制,先經過哪個過濾器,後經過哪個過濾器。在web.xml中,我們知道,執行順序是誰在前邊執行誰。 在spring boot中的F

Spring Boot通過Mybatis使用mapper介面和xml配置sql連線資料庫

由於最早使用的是註解的方式來連線資料庫,所以比較簡單,後來發行做後臺的同事用的一般都是xml的形式,所以也跟著學習了下。發行mapper介面和xml配置sql,比較複雜,尤其是對我這種新手,研究了好久才配置成功。為了防止忘記,故寫下來,也給準備學習的人當參考

Spring boot配置引數清單

轉自:http://wenku.baidu.com/link?url=LTN2p7D93t_2u6IBQeQZNL_hwiDfRNkcxI4duAqyC9uJhUmlA4VWieUcNWT8wZUq8f7aZh--UqGSpexKAtT3SalQdwIRKm67c9hN0x

Spring Boot 整合 Freemarker50 多行配置是怎麼省略掉的?

Spring Boot2 系列教程接近完工,最近進入修修補補階段。Freemarker 整合貌似還沒和大家聊過,因此今天把這個補充上。 已經完工的 Spring Boot2 教程,大家可以參考這裡: 乾貨|最新版 Spring Boot2.1.5 教程+案例合集 Freemarker 簡介 這是一個相當

Spring Boot第二彈配置檔案怎麼造?

持續原創輸出,點選上方藍字關注我吧 前言 自從用了Spring Boot,個人最喜歡的就是Spring Boot的配置檔案了,和Spring比起,Spring Boot更加靈活,修改的某些配置也是更加得心應手。 Spring Boot 官方提供了兩種常用的配置檔案格式,分別是properties、YML格