美文网首页
@InitBinder 注解的使用

@InitBinder 注解的使用

作者: MR崔先生 | 来源:发表于2021-07-21 09:20 被阅读0次

在使用SpingMVC框架的项目中,经常会遇到页面某些数据类型是Date、Integer、Double等的数据要绑定到控制器的实体,或者控制器需要接受这些数据,如果这类数据类型不做处理的话将无法绑定。
这里我们可以使用注解@InitBinder来解决这些问题,这样SpingMVC在绑定表单之前,都会先注册这些编辑器。一般会将这些方法些在BaseController中,需要进行这类转换的控制器只需继承BaseController即可。其实Spring提供了很多的实现类,如CustomDateEditor、CustomBooleanEditor、CustomNumberEditor等,基本上是够用的。

public class BaseController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new MyDateEditor());
        binder.registerCustomEditor(Double.class, new DoubleEditor()); 
        binder.registerCustomEditor(Integer.class, new IntegerEditor());
    }

    private class MyDateEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = format.parse(text);
            } catch (ParseException e) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = format.parse(text);
                } catch (ParseException e1) {
                }
            }
            setValue(date);
        }
    }

    public class DoubleEditor extends PropertiesEditor  {    
        @Override    
        public void setAsText(String text) throws IllegalArgumentException {    
            if (text == null || text.equals("")) {    
                text = "0";    
            }    
            setValue(Double.parseDouble(text));    
        }    

        @Override    
        public String getAsText() {    
            return getValue().toString();    
        }    
    }  

    public class IntegerEditor extends PropertiesEditor {    
        @Override    
        public void setAsText(String text) throws IllegalArgumentException {    
            if (text == null || text.equals("")) {    
                text = "0";    
            }    
            setValue(Integer.parseInt(text));    
        }    

        @Override    
        public String getAsText() {    
            return getValue().toString();    
        }    
    }  
} 

相关文章

  • Springmvc 日期时间提交

    form表单提交日期使用@InitBinder注解@InitBinder() public void ini...

  • @InitBinder 注解的使用

    在使用SpingMVC框架的项目中,经常会遇到页面某些数据类型是Date、Integer、Double等的数据要绑...

  • Spring注解:InitBinder

    注解 InitBinder 是用来初始化绑定器Binder的,而Binder是用来绑定数据的,换句话说就是将请求参...

  • springmvc

    1. @InitBinder 注解一个方法,用来初始化属性编辑器、注册参数验证器 1.1 属性编辑器,指定了类型和...

  • Springboot 格式化时间

    1.@InitBinder 标签对表单数据绑定, @InitBinder有什么作用 springMVC中bean中...

  • java注解

    内容: 注解的定义 注解的语法 源码级别的注解的使用 运行时注解的使用 编译时注解的使用 Android 预置的注...

  • 【JAVA】注解

    元注解 用来定义、声明注解的注解。 @Inherited注解 使用此注解声明出来的自定义注解,在使用此自定义注解时...

  • Controller层的统一异常处理(@ControllerAd

    要将自己的系统异常进行细分,具体是业务异常还是其他什么异常 使用@InitBinder,日期编辑器兼容日期,时间和...

  • springboot + shiro 权限注解、统一异常处理、请

    shiro注解的使用 shiro权限注解 Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AO...

  • Java注解的使用

    Java注解的使用 参考 廖雪峰java教程 使用注解 什么是注解(Annotation)?注解是放在Java源码...

网友评论

      本文标题:@InitBinder 注解的使用

      本文链接:https://www.haomeiwen.com/subject/zhodmltx.html