【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder

作者:ssslinppp      

1. 摘要

类型转换器常用于转换double、float、date等类型。
上文讲解了Converter类型转换器,这属于Spring 3新支持的类型转换器;

本文主要介绍@InitBinder,可参考如下链接:
参考:  http://blog.csdn.net/axin66ok/article/details/17938095 
功能说明:
当表单提交double、date等类型时,我们需要将其转换为java可识别的date,double等,如在浏览器中输入:
http://localhost:8080/SpringMVCTest/test/conversionTest.action?person=zhangsan:666:ssss:3.1415  时,
需要将其转化到Person对象中,此时需要对double类型进行转换。
【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder_第1张图片

2. 定义转换器类

【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder_第2张图片
   
    
    
    
    
  1. package com.ll.model;
  2. import java.beans.PropertyEditorSupport;
  3. public class PersonEditor extends PropertyEditorSupport {
  4. @Override
  5. public void setAsText(String text) throws IllegalArgumentException {
  6. Person p = new Person();
  7. if (text != null) {
  8. String[] items = text.split(":");
  9. p.setUsername(items[0]+"by propertyeEditor");
  10. p.setPasswd(items[1]);
  11. p.setRealName(items[2]);
  12. p.setPrice(Double.parseDouble(items[3]));
  13. }
  14. setValue(p);
  15. }
  16. @Override
  17. public String getAsText() {
  18. return getValue().toString();
  19. }
  20. }

3. 注册自定义编辑器



4. 类型转换测试


在浏览器中输入:
【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder_第3张图片
类型转换器会自动将"person=zhangsan:666:ssss:3.1415"转换为Person对象;



5. 其他

http://blog.sina.com.cn/spstudy 
http://blog.sina.com.cn/spstudy 






来自为知笔记(Wiz)


你可能感兴趣的:(【Spring学习笔记-MVC-8.1】SpringMVC之类型转换@initBinder)