有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到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. GridView获取CheckBox的值及所在列的ID
  2. 浙江大学PAT上机题解析之1015. 德才论 (25)
  3. xheditor编辑器上传截图图片抓取远程图片代码
  4. C复习手记(Day1)
  5. Objective-C 类属性和方法的訪问权限
  6. MUI——页面的创建、显示、关闭
  7. 自学html5要花多长时间
  8. Sublime 常用快捷键
  9. python中的字符串编码
  10. 关于js中对象和函数的一道问题
  11. docker for ubuntu 18 安装
  12. CSRedisCore 在net core中的使用
  13. leetcode461
  14. [UE4]小技巧:自动添加函数返回值
  15. DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime
  16. java C# objective-c AES对称加解密
  17. BZOJ1087【状压DP】
  18. @JsonInclude(Include.NON_NULL)
  19. ServiceHelper
  20. request方法总结

热门文章

  1. genkins的报错排查
  2. Linux使用日志
  3. apache2 + django
  4. 微信公众号中的支付宝支付与微信支付 && 支付宝支付问题(微信bug)
  5. Java通过流对MP4视频文件进行加密,H5 video播放流
  6. 学习Python要知道哪些重要的库和工具
  7. js 打印指定页面部分打印
  8. type=file的change事件只能执行一次的解决方案
  9. Cheatsheet: 2017 05.01 ~05.31
  10. Python基础学习总结(六)