spring定制属性编辑器

<!-- 自定义编辑器配置器 -->
	<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<!-- 自定义编辑器集 -->
		<property name="customEditors">
			<map>
				<entry key="cn.itcast.spring.scatter.Address">
					<bean class="cn.itcast.spring.editor.AddressEditor"/>
				</entry>
			</map>
		</property>
	</bean>
	
	<bean id="scatter" class="cn.itcast.spring.scatter.Scatter">
		<property name="comAddress">
			<bean class="cn.itcast.spring.scatter.Address">
				<property name="province" value="jilin"/>
				<property name="city" value="cc"/>
				<property name="street" value="${jdbc.url}"/>
			</bean>
		</property>
		<property name="homeAddress">
			<value>jilin.cc.renmin.100000</value>
		</property>
	</bean>
public class Scatter {
	
	private Address comAddress; // 公司住址
	private Address homeAddress; // 家庭住址

	public Address getComAddress() {
		return comAddress;
	}

	public void setComAddress(Address comAddress) {
		this.comAddress = comAddress;
	}

	public Address getHomeAddress() {
		return homeAddress;
	}

	public void setHomeAddress(Address homeAddress) {
		this.homeAddress = homeAddress;
	}
}
public class AddressEditor extends PropertyEditorSupport {

	public void setAsText(String text) throws IllegalArgumentException {
		if (text != null && text.length() > 0) {
			String[] ss = text.split("\\.");
			if(ss.length>3) {
				Address a = new Address();
				a.setProvince(ss[0]);
				a.setCity(ss[1]);
				a.setStreet(ss[2]);
				a.setZipCode(ss[3]);
				setValue(a);
			} else {
				setValue(null);
			}
		} else {
			setValue(null);
		}
	}
}

相关推荐