ApplicationContext more powerful Features-Learn notes

Source: Internet
Author: User
Tags locale

---restore content starts---

First, international support

II. Resource access

Iii. Transmission of events

Internationalization support

ApplicationContext inherits the Org.springframework.context.MessageResource interface and uses the various methods of getmessage () to obtain information resources.

So as to achieve the purpose of international information. GetMessage () has three methods:

1) string GetMessage (string code,object[] args,string Default,locale Loc)

2) string GetMessage (String code,object[] Args,locale Loc)

3) String getMessage (messagesourceresolvable resolvable,locale Locale), pass messagesourceresolvable to the code that needs to obtain the information

When ApplicationContext is loaded, it automatically looks for the Messagesource,spring convention defined in the XML that the bean must be defined as Messagesource.

Developers can use Org.springframework.context.support.ResourceBundleMessageSource to get international information.

Examples are as follows:

1) Define spring's Configuration document Config

2) Define the document that holds the information resource

3) write a test program to view the output

1.config.xml

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE beans Public "-//spring//dtd bean//en"

"Http://www.springframework.org/dtd/spring-beans.dtd" >

<beans>

<!--responsible for internationalization support--

<bean id= "Messagesource" class= "Org.springframework.context.support.ResourceBundleMessageSource" >

<property name= "basename" >

<!--internationalization support is defined in a folder with a file name of message--

<value>messages</value>

</property>

</bean>

<bean id= "HelloWorld" class= "Com.gc.action.HelloWorld" ></bean>

<bean id= "Date" class= "Java.util.Date"/>

</beans>

The name of the document that holds the information resource is messages.properties or Messages.class

2. Write the document Messages.properties that holds the information resource in Notepad.

Helloworld= greeting: {0} Greeting time: {1}

{0} {1} is used to identify where incoming values are stored when parameters are passed in from outside

Store the messages.properties under the Classpath, MYAPP\WEB-INF\SRC

3. Test procedure Testhelloworld.java

testhelloworld.java*********

Package com.gc.test;

Import Java.util.Calendar;

Import Java.util.Locale;

Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.FileSystemXmlApplicationContext;

Import Com.gc.action.HelloWorld;

public class testhelloworld{

public static void Main (string[] args) throws Instantiationexception,illegalaccessexception,

classnotfoundexception{

Obtaining a configuration document via ApplicationContext

ApplicationContext actx=new filesystemxmlapplicationcontext ("config");

Set the current time

Object[] Objs=new object[] {"HelloWorld", Calendar.getinstance (). GetTime ()};

Internationalization support

String msg=actx.getmessage ("HelloWorld", Objs. Local.china)

SYSTEM.OUT.PRINTLN (msg);

}

}

Description

1.OBJS is an array that holds the content to be passed in, and the contents of the array correspond to the {0} and {1} in the Messages.properties, respectively

2. The result of the output, Chinese is garbled: because Java itself in the transcoding process occurred problems

Workaround:

CMD input cd\ back to the C packing directory, then enter NATIVE2ASCII messages.properties messages.txt

View the C-Drive generated messages.txt file, copy the content into the messages.properties, overwrite the original can

Create a new message_en_us.properties file in the same way under MYAPP/WEB-INF/SRC input: helloworld=language{0} Time{1}

Modify the test program Testhelloworld, change Locale.china to Locale.us

Summary: You can easily implement internationalization support by simply modifying the locale

Resource access

ApplicationContext inherits the Reourceloader interface, and developers can use the GetResource () method and specify the URL of the resource file to get

ApplicationContext There are 3 ways to store resource files in the path of the set resource file:

1. virtual path to access;

2. Absolute path to access;

3. Relative path to access.

1. resource file located under Classpath: classpath: Is the URL virtual path agreed by spring

ApplicationContext actx=new filesystemxmlapplicationcontext ("config");

Resource Resource=actx.getresource ("classpath:messages.properties");

2. Actual path: File: or http:

ApplicationContext actx=new filesystemxmlapplicationcontext ("config");

Resource Source = Actx.getresource ("file:d:/eclipse/workspace/myapp/web-inf/src/messages.properties");

3. Relative path

ApplicationContext actx=new filesystemxmlapplicationcontext ("config");

Resource Source = Actx.getresource ("web-inf/src/messages.properties");

When a resource is obtained, the developer can use:

GetFile () to access the contents of the resource file

Exists () to check if the resource file exists

IsOpen () Check if the resource file is open

GetURL () returns the URL of the resource file

Event delivery: To Be Continued ...

---restore content ends---

ApplicationContext more powerful Features-Learn notes

Related Article

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.