SpringMVC中利用@InitBinder来对页面数据进行解析绑定

news/2024/7/5 19:59:21

  同步发布:http://www.yuanrengu.com/index.php/springmvc-user-initbinder.html

  在使用SpingMVC框架的项目中,经常会遇到页面某些数据类型是Date、Integer、Double等的数据要绑定到控制器的实体,或者控制器需要接受这些数据,如果这类数据类型不做处理的话将无法绑定。

      这里我们可以使用注解@InitBinder来解决这些问题,这样SpingMVC在绑定表单之前,都会先注册这些编辑器。一般会将这些方法些在BaseController中,需要进行这类转换的控制器只需继承BaseController即可。其实Spring提供了很多的实现类,如CustomDateEditor、CustomBooleanEditor、CustomNumberEditor等,基本上是够用的。

       demo如下:

 

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(); } } }

 

 

 


http://www.niftyadmin.cn/n/2436254.html

相关文章

躺玩手机险些酿成大祸 你知道危害有多大吗?

最近,浙江一名小伙子,他到朋友家玩,躺在朋友家床上,举着手机。突然间他不自觉地手一松,手机“啪嗒”掉下来,刚好砸进他角膜移植后的右眼,险些失明。还好及时送进医院救治,脱离了危险…

spring aop 创建代理

为什么80%的码农都做不了架构师?>>> spring aop 创建代理 我们知道spring org\springframework\aop\framework\DefaultAopProxyFactory.java#createAopProxy的方法返回的JdkDynamicAopProxy 或 CglibProxyFactory 实现了创建代理类的逻辑,分…

BZOJ 3542 [Poi2014]Couriers ——可持久化线段树

【题目分析】 查找区间内出现次数大于一半的数字。 直接用主席树&#xff0c;线段树上维护区间大小&#xff0c;由于要求出现次数大于一半&#xff0c;每到一个节点可以分治下去。 时间复杂度(NQ)logN 【代码】 #include <cstdio> #include <cstring> #include <…

Linux系统从指定目录下的所有文件中查找某个关键字

2019独角兽企业重金招聘Python工程师标准>>> 命令 find test/ -name *.* | xargs grep "hello" 该命令主要应用于仅记住了某个文件中的部分内容&#xff0c;但已不记得该文件的具体名字&#xff0c;或者所在路径&#xff0c;那么可以使用此命令根据 记住的…

JAVA NIO简单实现Socket Server

为什么80%的码农都做不了架构师&#xff1f;>>> 使用JAVA NIO简单实现Socket Server package com.flyer.cn.javaIO;import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.nio.ByteBuffer; import java.nio…

LeetCode-448. Find All Numbers Disappeared in an Array C#

Given an array of integers where 1 ≤ a[i] ≤ n (n size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You…

MyBatisPlus 入门学习笔记(版本:3.4.3)

文章目录学习辅助资料MyBatisPlus概述1. MyBatisPlus是什么2. 特性快速开始1. 创建数据库 mybatis_plus2. 导入相关依赖3. 数据源配置3. 快速开始3.1 User实体类编写3.2 mapper编写3.3 启动类设置3.4 测试配置日志Mapper层自带的的CRUD方法1. Insert插入操作1.1 产生奇妙ID的原…

利用反射机制获取属性的值遇到的坑

类&#xff1a; public Class Test { public string name; public string value; } Test tnew Test(); t.name"abc"; t.value"123"; string str(string)t.GetType().GetProperty("name").GetValue(t,null); 找了3个小时&#xff0c;都找不出问题…