天天看点

如何定义属性编辑器?

* 如何定义属性编辑器?

* 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java

* 将属性编辑器注册到spring中,参见:applicationContext-editor.xml

  1. publicclassUtilDatePropertyEditorextendsPropertyEditorSupport
  2. {
  3. privateStringformat="yyyy-MM-dd";
  4. publicvoidsetFormat(Stringformat)
  5. {
  6. this.format=format;
  7. }
  8. @Override
  9. publicvoidsetAsText(Stringtext)throwsIllegalArgumentException
  10. {
  11. System.out.println("-------------text--------:"+text);
  12. SimpleDateFormatsdf=newSimpleDateFormat(format);
  13. try
  14. {
  15. Dated=(Date)sdf.parse(text);
  16. this.setValue(d);
  17. }
  18. catch(ParseExceptione)
  19. {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  1. <propertyname="dateValue"value="2000-12-4"></property>
  1. <beanid="customEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  2. <propertyname="customEditors">
  3. <map>
  4. <entrykey="java.util.Date">
  5. <beanclass="com.spring.UtilDatePropertyEditor"></bean>
  6. </entry>
  7. </map>
  8. </property>
  9. </bean>