转自http://www.cnblogs.com/douJiangYouTiao888/p/6765220.html

有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到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));
}
}

  

最新文章

  1. 基于pcDuino-V2的无线视频智能小车 - pcduino上的网络编程
  2. 【简易版】HashMap(增删改查)
  3. mysql--1130ERROR
  4. 线性函数拟合R语言示例
  5. JS初学之-选项卡(常见)
  6. Realtek 8168 安装 VMware ESXi 提示没有驱动
  7. centos6.7下网络设置
  8. Java计算文件的SHA码和MD5码
  9. uvalive 2797 Monster Trap
  10. 从linux telnet到exchange邮件server来測试发送邮件
  11. IOS 类的属性修饰符atomic
  12. easyUI parser的使用
  13. 第三章 Docker的镜像
  14. 苹果iPhone X上搭载的那颗A11仿生芯片,到底牛在哪?
  15. MySQL Innodb如何找出阻塞事务源头SQL
  16. 17秋 软件工程 团队第五次作业 Alpha Scrum1
  17. 智能优化 之 下山单纯形法 C++
  18. socket-重叠模型(overlap)
  19. drp错误集锦---“Cannot return from outside a function or method”
  20. .net页面生命周期【转】

热门文章

  1. ext container的使用的场景
  2. 通过jmeter发送webservice接口请求
  3. 1、JAVA的小白之路
  4. java虚拟机学习笔记(六)---垃圾收集算法
  5. Linux常用命令之权限管理
  6. 使用appscan安全扫描问题以及解决办法
  7. 对平底锅和垃圾的O奖论文的整理和学习[2](2018-02-08发布于知乎)
  8. PKI机制总结
  9. Mybatis案例超详解(上)
  10. 【Isabella Message】 【SPOJ - ISAB】【模拟】【矩阵的旋转】