Junky's IT Notebook

          統(tǒng)計(jì)

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評(píng)論排行榜

          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

          然后注入資源文件(一個(gè)名字為basename的屬性),然后就可以在Context中使用資源文件了, 如下為一個(gè)配置示例: 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

          下面是一個(gè)簡(jiǎn)單的測(cè)試類:

          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);

          ? }

          }

          很簡(jiǎn)單,這樣就可以了.

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

          888-666-9999

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

          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;
          ?? }
          }

          然后定義一個(gè)用來(lái)編輯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里面的屬性編輯器,實(shí)現(xiàn)里面的一個(gè)方法就可以了, 下面就是在配置文件中注冊(cè)該編輯器.如下:

          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 >

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

          Contact的PhoneNumber的屬性

          -->

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

          < property name = "phoneNumber" >

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

          </ property >

          </ bean >

          </

          beans >

          最后來(lái)測(cè)試一下注冊(cè)的結(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, 很簡(jiǎn)單,下一次來(lái)看看,Spring提供的一下比較有意思的功能.如定時(shí),發(fā)送Email等.

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

          主站蜘蛛池模板: 桃园县| 惠东县| 富宁县| 瑞安市| 兴宁市| 安达市| 深水埗区| 从江县| 鲜城| 林州市| 钦州市| 丹阳市| 东乡族自治县| 遂宁市| 常山县| 潜江市| 阿拉善盟| 丰镇市| 蓬溪县| 宜黄县| 婺源县| 岢岚县| 武隆县| 广德县| 义乌市| 九龙坡区| 和林格尔县| 牟定县| 平度市| 宜宾市| 颍上县| 工布江达县| 临清市| 柘城县| 万荣县| 沅陵县| 玉林市| 阳谷县| 五常市| 海宁市| 西昌市|