Spring Getting Started 13 - MessageResource Interface

xiaoxiao2021-03-06  22

Getting Started 13 - MessageResource Interface

ApplicationContext inherits the Org.SpringFramework.Context.MessageResource interface, you can use GetMessage () to obtain information resources to achieve international information purposes. In this topic, we have made an international message through a live org.springframework.context.support.resourceBund.Context.support.resourceBundleMessageSource, which is in the MessageResource, first writing in the bean defined file:

Messages

The BaseName property is used to set the front file name of the message resource. The setting is Messages here, the name of the message resource file can be Messages_en_us.properties, Messages_Zh_TW.Properties, Messages_ *. Properties, etc., here we Provide two: messages_en_us.properties, Messages_ZH_TW.Properties, first Messages_en_us.properties content is as follows:

UserLogin = User {0} login at {1}

Message_zh_tw.properties content is as follows:

UserLogin = user {0} on {1} login

Where {0} and {1} are the parameters we can set during the execution of the program. Spring actually uses JDK's ResourceBundle to read these files, and Chinese will have problems during the encoding process. In order to display Chinese correctly, we must encode the message_zh_tw.properties, using the Native2ASCII in the JDK tool. :

Native2ASCII Message_zh_tw.properties Message_zh_tw.txt

After that, the message_zh_tw.txt overwritten (replaced) will be off the message_zh_tw.properties. Next, write a simple test program:

Package online.

Import java.util. *;

Import org.springframework.context. *;

Import org.springframework.context.support. *;

Public class test {

Public static void main (string [] args) throws exception {

ApplicationContext CONTEXT = New classpathxmlapplicationContext ("bean.xml"); object [] arguments = new object [] {"葛 格", Calendar.GetInstance (). GetTime ()};

System.out.println (Context.getMessage ("UserLogin", Arguments, Locale.us);

System.out.println (Context.getMessage ("UserLogin", Arguments, Locale.taiwan);

}

}

ClassPathXMLApplicationContext implements the ApplicationContext interface, we use it to read the bean definition file, and return an instance of an ApplicationContext interface, through the setting parameter and specify local, we use the getMessage () set the corresponding message, this test program results Show the following content:

Log4J: Warn no appenders could be found for logger (org.springframework.beans.Factory.xml.xmlbeandefinitionReader).

Log4J: Warn Please Initialize The log4j system prot in.

User Liang Zer Login AT 10/28/04 12:52 PM

Users Liang Zigue login in 2004/10/28 at 12:52 pm

转载请注明原文地址:https://www.9cbs.com/read-60888.html

New Post(0)