表单中的日期 字符串和Javabean中的日期类型的属性自动转换
搞了一上午的bug最终还是因为自己springMVC的注解不熟悉的原因,特记录。
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能。
比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作
有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到Controller里的Date参数。需要使用@initBinder注解为binder提供一个数据的转换器,这个转换器可以自己实现,也可以用spring官方的一些实现。比如:

package com.wang.action; import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 测试@initBinder注解
* @author wlyfree
*/
@Controller
public class BinderAction { @RequestMapping("/sb2.do")
public void doTest(@RequestParam(value="name")String name,@RequestParam(value="age")double age,@RequestParam(value="nowTime")Date nowTime){
System.err.println("name:" + name);
System.err.println("age:" + age);
System.err.println("nowTime:" + nowTime);
} @InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}

表单中的日期 字符串和Javabean中的日期类型的属性自动转换的更多相关文章
- springMVC注解@initbinder日期类型的属性自动转换
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 一 ...
- SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换
SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...
- 将某个日期字符串转换为java.sql.Date的类型
import java.text.ParseException; import java.text.SimpleDateFormat; public class date { /** * @param ...
- 日期字符串解析--SimpleDateFormat严格限制日期转换setLenient(false)
输入“33/12/2011”,用SimpleDateFormat parse()方法,转化为Date(2012,01,02).这样处理相当“33/12/2011”是正常输入,如果需要"33/ ...
- 数据库表中存在Text类型的属性时,写sql语句时需要注意喽!
之前,习惯性地写查询语句时,查询条件用“=”判断.今天写程序的时候,查询时突然报了一个错误:数据类型text 和varchar 在equal to 运算符中不兼容.查看了一下数据库发现,其中有一个属性 ...
- C#中把任意类型的泛型集合转换成SQLXML数据格式的小例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 在Git中一定要关注的crlf自动转换
GitHub 第一坑:换行符自动转换 如果你已经做出了错误的选择,也不需要重新安装,可以直接使用命令行来修改设置.很简单,直接打开这货自带的命令行工具 Git Bash,输入以下命令,再敲回车即可: ...
- html中属于布尔类型的属性
1.noshade,用来表示有无阴影,多用于在<hr />标签当中 2.ckecked,用来表示是否默认选中,多用于单选按钮<input type="radio" ...
- IntelliJ IDEA 把Json字符串 增加到IDE里 用windows记事本 能自动转换(自动增加转义字符)
随机推荐
- ACM_Uppercase(水题)
Uppercase Time Limit: 2000/1000ms (Java/Others) Problem Description: 给出一句中间无特殊符号的英语句子,要求将各单词的首字母改大写. ...
- magento后台开发学习笔记(入门实例向)
目的是做一个grid,参考博客http://www.sunzhenghua.com/magento-admin-module-development-part1-grid-forms-tabs-con ...
- 专题七:UDP编程补充——UDP广播程序的实现
一.程序实现 UDP广播程序的实现代码: using System; using System.Net; using System.Net.Sockets; using System.Text; us ...
- C:\Windows\System32\drivers\etc\hosts文件显示
attrib -s -h C:\Windows\System32\drivers\etc\hosts
- 如何删除sublime目录
左侧栏的sublime目录一直删不掉,删除列直接变成了灰色. 今天才发现应该选择文件夹右击选择工程——从工程中删除文件夹. 这个设计真的很醉,删除这么常用的键还放进了第二层……
- 2017-12-01HTML块及引用
HTML块1.HTML块元素 快元素在显示时,通常会以新行开始 例如:<h1>.<p>.<ul>2.HTML内联元素 内联元素通常不会以新行开始 例如:<b& ...
- esp8266 SOC方案经过半年沉淀之后再度重启二
2018-08-2014:16:10 以下是输出控制 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); GPIO_OUTPUT_SET ...
- JBoss4.2的启动方式-Jboss无法通过IP地址访问,只能用localhost访问
JBOSS版本:4.2.3GA症状:服务器无法通过IP地址去访问,只能用127.0.0.1或者localhost来访问. 开始怀疑是端口没有放开,用telnet ip 80 也不能连接,就一直怀疑端口 ...
- Clickhouse DDL&DML
(1)添加列: alter table [db.]table_name add column column_name [type] [default_expr] [after name_after] ...
- MySQL(四)DQL语言——条件查询
摘要:条件查询:条件表达式,逻辑表达式,模糊查询,like,通配符,转义字符,escape关键字,between and,in,is null,is not null,安全等于. 条件查询语法: SE ...