Spring strategy learning notes (2.13) -- parse text messages

Source: Internet
Author: User

I. knowledge points

Spring's application context can parse text messages for the target language environment based on keywords. Generally, messages in a language environment should be stored in an independent property file, which is calledResource bundle).

Messagesource is an interface that defines multiple message parsing methods. The applicationcontext interface extends this interface so that all application context can parse text messages. The application context delegates message parsing to the bean named messagesource. Resourcebundlemessagesource is the most common implementation of messagesource, which parses messages in different language environments from the resource package.

Ii. Sample Code

Create the US English resource package message_en_us.properties.

alert.checkout=A shopping cart has been checked out.

Bean Configuration

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-co Ntext-3.2.xsd "> <! -- Use resourcebundlemessagesource as the implementation of messagesource. The bean name must be set to messagesource, so that the application context can discover it. You must specify the basic name of the resource package for resourcebundlemessagesource --> <bean id = "messagesource" class = "org. springframework. context. support. resourcebundlemessagesource "> <property name =" basename "> <value> messages </value> </property> </bean> </beans>

For this messagesource definition, if you are looking for a text message in the United States, the preferred language is English, you will first consider the resource package messages_en_us.properties that matches the language and country. If such a resource package or message is not found, only messages_en.properties of the language will be considered. If not, the default resource package messages. properties for all regions will be selected.

 

Test class

Package COM. jackie. codeproject. springrecipesnote. springadvancedioc; import Java. util. locale; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext;/*** @ author Jackie **/public class messageresolvetest {@ testpublic void testmessageresolve () {applicationcontext = new classpathxmlapplica Tioncontext ("applicationcontext. xml"); // The getmessage method requires the application context to parse the message. The first parameter is the keyword, and the third parameter is the target language environment. String Alert = applicationcontext. getmessage ("alert. Checkout", null, locale. US); system. Out. println (alert );}}

The second parameter of the getmessage () method is the Message Parameter array. You can use index values to define multiple parameters.

alert.checkout=A shopping cart costing {0} dollars has been checked out at {1}.

Input an object array. The array elements are converted to strings before entering the parameter.

String alert = applicationContext.getMessage("alert.checkout", new Object[]{4, new Date()}, Locale.US);System.out.println(alert);

For beans that parse text messages, the applicationcontextaware interface or messagesourceaware must be implemented.

 

 

Package COM. codeproject. jackie. springrecipesnote. springadvancedioc; import Java. io. bufferedwriter; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. util. date; import Java. util. locale; import Org. springframework. beans. factory. beannameaware; import Org. springframework. context. messagesource; import Org. springframework. context. messagesourceaware;/*** cashier class implements beannameaware aware interfaces and storageconfig flag interfaces and messagesourceaware interfaces * @ author Jackie **/public class cashier implements beannameaware, storageconfig, messagesourceaware {private string name; private bufferedwriter writer; private string path; private messagesource; Public void setpath (string path) {This. path = path;} public void openfile () throws ioexception {file = new file (path, name + ". TXT "); filewriter fw = new filewriter (file, true); writer = new bufferedwriter (FW);} public void checkout (shoppingcart cart) throws ioexception {double Total = 0; for (product: cart. getitems () {total + = product. getprice ();} writer. write (new date () + "\ t" + total + "\ r \ n"); writer. flush (); string Alert = messagesource. getmessage ("alert. checkout ", new object [] {total, new date ()}, locale. US); system. out. println (alert);} public void closefile () throws ioexception {writer. close () ;}@ overridepublic string getpath () {return path ;}@ overridepublic void setbeanname (string beanname) {This. name = beanname ;}@ overridepublic void setmessagesource (messagesource) {This. messagesource = messagesource ;}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.