PropertyEditorSupport使用方法:



Java代码  收藏代码
  1. public class Person {  
  2.     private String name;  
  3.     private int id;  
  4.     private int age;  
  5.   
  6.     public int getAge() {  
  7.         return age;  
  8.     }  
  9.   
  10.     public void setAge(int age) {  
  11.         this.age = age;  
  12.     }  
  13.   
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(int id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29. }  
  30.   
  31. import java.beans.PropertyEditorSupport;  
  32. import org.springframework.util.StringUtils;  
  33.   
  34. public class BuildcusPE extends PropertyEditorSupport {  
  35.   
  36.     public void setAsText(String info) throws IllegalArgumentException {  
  37.         // super.setAsText(info);  
  38.         String[] personInfo = StringUtils.delimitedListToStringArray(info, "|");  
  39.         // 顺序为 id,age,name  
  40.         Person man = new Person();  
  41.         man.setId(Integer.parseInt(personInfo[0]));  
  42.         man.setAge(Integer.parseInt(personInfo[1]));  
  43.         man.setName(personInfo[2]);  
  44.         setValue(man);  
  45.     }  
  46. }  
  47.   
  48. import org.springframework.context.ApplicationContext;  
  49. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  50.   
  51. public class Testpe {  
  52.     private Person man;  
  53.   
  54.     public Person getMan() {  
  55.         return man;  
  56.     }  
  57.   
  58.     public void setMan(Person man) {  
  59.         this.man = man;  
  60.         System.out.println("员工姓名:" + man.getName());  
  61.         System.out.println("员工编号:" + man.getId());  
  62.         System.out.println("员工年龄:" + man.getAge());  
  63.     }  
  64.   
  65.     public static void main(String[] args) {  
  66.         ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc2-servlet.xml");  
  67.     }  
  68. }  
  69.   
  70.   
  71. <!-- Test PropertyEditorSupport -->  
  72.     <!-- Begin -->  
  73.     <bean name="customEditorConfigurer"  
  74.         class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  75.         <property name="customEditors">  
  76.             <map>  
  77.                 <entry key="com.alipay.yiyu.springmvc.testPropertyEditor.Person">  
  78.                     <bean class="com.alipay.yiyu.springmvc.testPropertyEditor.BuildcusPE"></bean>  
  79.                 </entry>  
  80.             </map>  
  81.         </property>  
  82.     </bean>  
  83.     <bean id="peTest" class="com.alipay.yiyu.springmvc.testPropertyEditor.Testpe">  
  84.         <property name="man">  
  85.             <value>3|29|David</value>  
  86.         </property>  
  87.     </bean>  
  88.     <!-- Test PropertyEditorSupport End -->  
原文地址:https://www.cnblogs.com/chenying99/p/2505395.html