Junky's IT Notebook

          統(tǒng)計

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評論排行榜

          Spring in Action 筆記 (IV) -- i18n問題和自定義屬性編輯器

          Spring in Action 筆記?(IV) -- i18n問題和自定義屬性編輯器

          ? BY: icess Blog: http://blog.matrix.org.cn/page/icess?

          ?? 在Spring中處理I18N問題和使用Java里面的類基本上是一樣的.使用org.springframework.context.support.ResourceBundleMessageSource

          然后注入資源文件(一個名字為basename的屬性),然后就可以在Context中使用資源文件了, 如下為一個配置示例: test.xml

          <?

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

          <!

          DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

          <

          beans >

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

          < property name = "basename" >

          <!-- 注意此處設(shè)置 資源 名字 和路徑 -->

          < value > test/i18n/test </ value >

          </ property >

          </ bean >

          </

          beans >

          下面為資源文件 test.properties

          name =

          \u51B0\u96E8

          sex =

          \u5148\u751F

          test_zh.properties

          name =

          \u51B0\u96E8

          sex =

          \u5148\u751F

          test_en_US.properties

          name =

          ice rain

          sex =

          male

          下面是一個簡單的測試類:

          package

          test.i18n;

          import

          java.util.Locale;

          import

          org.springframework.context.ApplicationContext;

          import

          org.springframework.context.support.ClassPathXmlApplicationContext;

          public

          class TestI18n {

          /**

          * @param args

          */

          ? public static void main(String[] args) {

          ??? // TODO Auto-generated method stub

          ??? ApplicationContext context =

          new ClassPathXmlApplicationContext( "test/i18n/test.xml" );

          ??? String text = context.getMessage(

          "sex" , new Object[0], Locale. US );

          ??? String textZH = context.getMessage(

          "sex" , new Object[0], Locale. CHINA );

          ??? System.

          out .println(text + " 中文:" +textZH);

          ? }

          }

          很簡單,這樣就可以了.

          下面來看看Spring中的屬性自定義編輯器,這個和Hibernate中的自定義屬性差不多 的. 例如下面我們要看到了例子,映射一個電話號碼,有areaCode,prefix和 number, 如果不使用自定義屬性編輯器那么就要分別注入上面的3個代碼,麻煩. 如果使用自定義屬性編輯器,直接注入一個-分開的數(shù)字序列就可以了 如

          888-666-9999

          .在下面的例子中的Contact.java類有個PhoneNumber屬性,里面保存了上面的3個代碼,兩個類的代碼如下:

          package? test.propertyEditor;

          public?class? Contact?{
          ?? private? PhoneNumber?phoneNumber;
          ?? private? String?name;
          ??
          ?? public? Contact()?{}
          ??
          ?? public? String?getName()?{
          ???? return? name;
          ?? }

          ?? public?void? setName(String?name)?{
          ???? this .name?=?name;
          ?? }

          ?? public? PhoneNumber?getPhoneNumber()?{
          ???? return? phoneNumber;
          ?? }

          ?? public?void? setPhoneNumber(PhoneNumber?phoneNumber)?{
          ???? this .phoneNumber?=?phoneNumber;
          ?? }
          ??
          }

          PhoneNumber.java

          package? test.propertyEditor;

          public?class? PhoneNumber?{
          ?? private? String?areaCode;
          ?? private? String?prefix;
          ?? private? String?number;
          ?? public? PhoneNumber()?{
          ????
          ?? }
          ?? public? PhoneNumber(String?areaCode,String?prefix,String?number)?{
          ???? this .areaCode?=?areaCode;
          ???? this .prefix?=?prefix;
          ???? this .number?=?number;
          ?? }
          ?? public? String?getAreaCode()?{
          ???? return? areaCode;
          ?? }
          ?? public?void? setAreaCode(String?areaCode)?{
          ???? this .areaCode?=?areaCode;
          ?? }
          ?? public? String?getNumber()?{
          ???? return? number;
          ?? }
          ?? public?void? setNumber(String?number)?{
          ???? this .number?=?number;
          ?? }
          ?? public? String?getPrefix()?{
          ???? return? prefix;
          ?? }
          ?? public?void? setPrefix(String?prefix)?{
          ???? this .prefix?=?prefix;
          ?? }
          }

          然后定義一個用來編輯PhoneNumber的編輯器PhoneEditor.java 如下:

          package? test.propertyEditor;

          import? java.beans.PropertyEditorSupport;

          public?class? PhoneEditor? extends? PropertyEditorSupport?{
          ?? public?void? setAsText(String?textValue)?{
          ???? String?stripped?=?stripNonNumber(textValue);
          ????
          ???? String?areaCode?=?stripped.substring( 0 , 3 );
          ???? String?prefix?=?stripped.substring( 3 , 6 );
          ???? String?number?=?stripped.substring( 6 );
          ???? PhoneNumber?phone?=? new? PhoneNumber(areaCode,prefix,number);
          ????
          ???? setValue(phone);
          ?? }
          ??
          ?? private? String?stripNonNumber(String?original)?{
          ???? StringBuilder?allNumeric?=? new? StringBuilder();
          ????
          ???? for ( int? i?=? 0 ;?i?<?original.length();?i?++)?{
          ?????? char? c?=?original.charAt(i);
          ?????? if (Character.isDigit(c))?{
          ???????? allNumeric.append(c);
          ?????? }
          ???? }
          ???? return? allNumeric.toString();
          ?? }
          }

          繼承java里面的屬性編輯器,實現(xiàn)里面的一個方法就可以了, 下面就是在配置文件中注冊該編輯器.如下:

          testPropertyEditor.xml

          <?

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

          <!

          DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

          <

          beans >

          < bean id = "customEditorConfigurer" class = "org.springframework.beans.factory.config.CustomEditorConfigurer" >

          < property name = "customEditors" >

          < map >

          < entry key = "test.propertyEditor.PhoneNumber" >

          < bean id = "phoneEditor" class = "test.propertyEditor.PhoneEditor" ></ bean >

          </ entry >

          </ map >

          </ property >

          </ bean >

          <!-- 如果不注冊上面自定義Editor的實現(xiàn), 需要注冊一個PhoneNumber的bean,設(shè)置其屬性然后再注冊

          Contact的PhoneNumber的屬性

          -->

          < bean id = "contact" class = "test.propertyEditor.Contact" >

          < property name = "phoneNumber" >

          < value > 888-666-9999 </ value >

          </ property >

          </ bean >

          </

          beans >

          最后來測試一下注冊的結(jié)果是否正確:

          package? test.propertyEditor;

          import? org.springframework.context.ApplicationContext;
          import? org.springframework.context.support.ClassPathXmlApplicationContext;

          public?class? TestPropertyEditor?{

          ?? /**
          ??? *? @param? args
          ??? */
          ?? public?static?void? main(String[]?args)?{
          ???? //?TODO?Auto-generated?method?stub
          ???? ApplicationContext?context?=? new? ClassPathXmlApplicationContext( "test/propertyEditor/testPropertyEditor.xml" );
          ???? Contact?c?=?(Contact)?context.getBean( "contact" );
          ????
          ???? System.out.println(c.getPhoneNumber().getAreaCode());
          ???? System.out.println(c.getPhoneNumber().getPrefix());
          ???? System.out.println(c.getPhoneNumber().getNumber());
          ?? }

          }

          ok, 很簡單,下一次來看看,Spring提供的一下比較有意思的功能.如定時,發(fā)送Email等.

          posted on 2006-05-29 23:19 junky 閱讀(385) 評論(0)  編輯  收藏 所屬分類: spring

          主站蜘蛛池模板: 科技| 馆陶县| 和田市| 德令哈市| 齐齐哈尔市| 洪洞县| 乌拉特后旗| 周宁县| 东平县| 偏关县| 哈尔滨市| 基隆市| 乌鲁木齐县| 星座| 万载县| 航空| 福清市| 澎湖县| 怀宁县| 府谷县| 太和县| 徐闻县| 霍州市| 阳新县| 日土县| 鄂温| 乳源| 本溪市| 南京市| 石门县| 宝清县| 绥滨县| 如东县| 辽阳市| 卢龙县| 平安县| 奉节县| 安陆市| 云霄县| 鹿泉市| 云安县|