ApplicationContext更強的功能-學習筆記

來源:互聯網
上載者:User

標籤:

---恢複內容開始---

一、國際化支援

二、資源訪問

三、事件傳遞

 

國際化支援

ApplicationContext繼承了org.springframework.context.MessageResource介面,使用getMessage()的各個方法來取得資訊資源

從而實現國際化資訊的目的。getMessage()有三個方法:

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),通過MessageSourceResolvable來傳入需要擷取資訊的代號

當ApplicationContext被載入的時候,它會自動尋找在XML中定義的messageSource,Spring約定這個Bean必須被定義為messageSource.

開發人員可以通過org.springframework.context.support.ResourceBundleMessageSource來取得國際化資訊。

執行個體如下:

1)定義Spring的配置文檔config.xml

2)定義存放資訊資源的文檔

3)編寫測試程式,查看輸出

 

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>

  <!--負責國際化支援-->

   <bean id=“messageSource” class="org.springframework.context.support.ResourceBundleMessageSource">

    <property name="basename">

          <!--國際化支援的定義在檔案名稱為message的檔案夾中-->

      <value>messages</value>

    </property>

  </bean>

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

  <bean id="date" class="java.util.Date"/> 

</beans>

存放資訊資源的文檔的名稱為messages.properties或messages.class

 

2.用記事本編寫存放資訊資源的文檔messages.properties.

HelloWorld=問候語:{0}  問候時間:{1}

{0} {1} 用來標識當從外部傳入參數時,傳入值存放的位置

把messages.properties存放在ClassPath下,即myApp\WEB-INF\src下

3.測試程式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{

  //通過ApplicationContext擷取配置文檔

  ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

  //設定目前時間

  Object[] objs=new Object[] {"HelloWorld",Calendar.getInstance().getTime()};

  //國際化支援

  String msg=actx.getMessage("HelloWorld",objs.Local.CHINA)

  System.out.println(msg);

}

}

說明:

1.objs是一個數組,用來存放要傳入的內容,數組中的內容分別對應與messages.properties中的{0}和{1}

2.輸出的結果,中文為亂碼:因為JAVA本身在轉碼過程中出現了問題

解決方案:

cmd中輸入cd\回到C盤根目錄,然後輸入native2ascii messages.properties messages.txt

查看C盤產生的messages.txt檔案,複製內容到messages.properties中,覆蓋原來的就可以

 

在myApp/WEB-INF/src下用同樣的方法建立一個message_en_US.properties檔案輸入:HelloWorld=Language{0} Time{1}

修改測試程式TestHelloWorld,將Locale.CHINA改為Locale.US

總結:只需要修改Locale就可以很容易地實現對國際化的支援

 

資源訪問

ApplicationContext繼承了ReourceLoader介面,開發人員可以使用getResource()方法並指定資源檔的URL來擷取

ApplicationContext對資源檔的儲存在設定資源檔的路徑上有如下3種方式:

1.虛擬路徑來存取;

2.絕對路徑來存取;

3.相對路徑來存取。

 

1.資源檔位於CLASSPATH下:classpath: 是Spring約定的URL虛擬路徑

ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

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

 

2.實際路徑:file:或http:

ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

Resource source = actx.getResource("file:d:/eclipse/workspace/myApp/WEB-INF/src/messages.properties");

 

3.相對路徑

ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

Resource source = actx.getResource("WEB-INF/src/messages.properties");

 

當取得一個Resource後,開發人員可以使用:

getFile() 來存取資源檔內容

exists()來檢查資源檔是否存在

isOpen()檢查資源檔是否被開啟

getURL()返回資源檔的URL

 

事件傳遞:  待續...

 

 

 

 

 

 

---恢複內容結束---

ApplicationContext更強的功能-學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.