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" >
<!-- 注意此处设置 资源 名字 和路径 -->
< value > test/i18n/test </ value >
</ property >
</ bean >
</
beans >下面为资源文件 test.properties
name =
\u51B0\u96E8
sex =
\u5148\u751Ftest_zh.properties
name =
\u51B0\u96E8
sex =
\u5148\u751Ftest_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个代码,麻烦. 如果使用自定义属性编辑器,直接注入一个-分开的数字序列就可以了 如
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里面的属性编辑器,实现里面的一个方法就可以了, 下面就是在配置文件中注册该编辑器.如下:
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的实现, 需要注册一个PhoneNumber的bean,设置其属性然后再注册
Contact的PhoneNumber的属性
-->
< bean id = "contact" class = "test.propertyEditor.Contact" >
< property name = "phoneNumber" >
< value > 888-666-9999 </ value >
</ property >
</ bean >
</
beans >最后来测试一下注册的结果是否正确:
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提供的一下比较有意思的功能.如定时,发送Email等.