Spring 自定义propertyEditor的示例代码


User
package com.example.zylspringboot.selfEditor;
public class User {
private String name;
private Address address;
private Integer age;
public String getName(android) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", address=" + address +
", age=" + age +
'}';
}
}
Address
package com.example.zylspringboot.selfEditor;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
thisjavascript.city = city;
}
@Override
public String toString() {
return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}';
}
}
SelfPropertyEditor
package com.example.zylspringboot.selfEditor;
import Java.beans.PropertyEditorSupport;
public class SelfPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] s = text.split("_");
Address address = new Address();
address.setCity(s[0]);
address.setProvince(s[1]);
super.setValue(address);
}
}
AcaakPropertyRegistor
package com.example.zylspringboot.selfEditor;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
public class AcaakPropertyRegistor implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
propertyEditorRegistry.registerCustomEditor(Address.class,new SelfPropertyEditor());
}
}
XML
<?xml version="1.0" encoding="UTF-8http://www.devze.com"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.orgjavascript/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.example.zylspringboot.selfEditor.User">
<property name="age" value="18"></property>
<property name="name" value="Acaak"&javascriptgt;</property>
<property name="address" value="广东省_广州市"></property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="com.example.zylspringboot.selfEditor.AcaakPropertyRegistor"></bean>
</list>
</property>
</bean>
或
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.example.zylspringboot.selfEditor.Address">
<value>com.example.zylspringboot.selfEditor.SelfPropertyEditor</value>
</entry>
</map>
</property>
开发者_JS学习</bean>
</beans>
到此这篇关于Spring 自定义propertyEditor的文章就介绍到这了,更多相关Spring 自定义propertyEditor内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
加载中,请稍侯......
精彩评论