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