<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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è)簡單的測試類:

    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中的屬性自定義編輯器,這個(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è)用來編輯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è)方法就可以了, 下面就是在配置文件中注冊該編輯器.如下:

    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的實(shí)現(xiàn), 需要注冊一個(gè)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提供的一下比較有意思的功能.如定時(shí),發(fā)送Email等.

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

    主站蜘蛛池模板: 亚洲人成亚洲人成在线观看| 亚洲乱码在线播放| 18禁止看的免费污网站| 亚洲w码欧洲s码免费| 亚洲国产精品碰碰| 久久国产色AV免费看| 亚洲成在人线aⅴ免费毛片| 亚洲精品无码不卡在线播HE| 日本免费网址大全在线观看| 特a级免费高清黄色片| 666精品国产精品亚洲| 亚洲成aⅴ人片久青草影院| 污视频在线免费观看| 九九精品国产亚洲AV日韩| 亚洲av综合av一区| 日韩毛片免费在线观看| 永久在线观看免费视频 | 中文字幕亚洲第一| 亚洲免费在线观看视频| 视频免费1区二区三区| 亚洲中文字幕一二三四区苍井空| 亚洲桃色AV无码| 日韩a在线观看免费观看| 99国产精品视频免费观看| 日韩在线观看免费| 中国china体内裑精亚洲日本| 亚洲精品午夜国产VA久久成人 | 亚洲熟妇无码另类久久久| 岛国片在线免费观看| 24小时在线免费视频| 国产乱妇高清无乱码免费| 亚洲日韩久久综合中文字幕| 亚洲综合亚洲国产尤物| 亚洲午夜久久久久久噜噜噜| 国产亚洲福利一区二区免费看| 久久精品免费一区二区| 日韩电影免费在线观看| 国产黄色免费观看| 国产亚洲精品国产福利在线观看| 亚洲色无码专区一区| 久久久亚洲AV波多野结衣|