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等.