spring自动类型转换========Converter和PropertyEditor
Spring有两种自动类型转换器,一种是Converter,一种是propertyEditor。
两者的区别:Converter是类型转换成类型,Editor:从string类型转换为其他类型。
从某种程度上,Converter包含Editor。如果出现需要从string转换到其他类型。首选Editor。
Converter代码展示:
实现string类型转换Date。
MyConverter类
public class MyConverter implements Converter<String, Date> {
public Date convert(String source) {
System.out.println("进入了 converter");
//创建类型转换器
@SuppressWarnings("unused")
SimpleDateFormat simpleDateFormat = getSimpleDateFormat(source);
Date date = null;
try {
date = simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
private SimpleDateFormat getSimpleDateFormat(String source) {
SimpleDateFormat simpleDateFormat;
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
} else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {
simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
} else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {
simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
} else {
throw new TypeMismatchException("", Date.class);
}
return simpleDateFormat;
}
}
Controller类
@Controller
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/add")
public ModelAndView add(Student student) {
System.out.println(student.getName());
System.out.println(student.getBirthday());
return new ModelAndView("success"); } @ExceptionHandler
public ModelAndView exceptionMethod(Exception ex, ModelAndView mv, HttpServletRequest request) {
System.out.println("进入新增界面");
//获取前台输入的信息
String name = request.getParameter("name");
String birthday = request.getParameter("birthday");
String message = ex.getMessage();
if (message.contains(name)) {
mv.addObject("nameerro", "用户名输入有误"); }
if (message.contains(birthday)) {
mv.addObject("birthdayerro", "日期输入有误");
}
mv.addObject("name", name).addObject("birthday", birthday).setViewName("index");
return mv;
} }
纠结了一下,还是决定写一下Editor的代码,然后打一局魂斗罗,希望多年后的自己还可以这么喜欢这款游戏。
尴尬了,原来还可以加行号。。。
@Controller
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/add")
public ModelAndView add(Student student) {
System.out.println(student.getName());
System.out.println(student.getBirthday());
return new ModelAndView("success"); } /**
* binder.registerCustomEditor初始化参数的绑定 newcustomDateEditor:创建类型编辑器 true
* 允许日期格式为空
*
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new MyEditor());
}
}
public class MyEditor extends PropertiesEditor {
@Override
public void setAsText(String source) throws IllegalArgumentException {
SimpleDateFormat sdf = getDate(source);
Date parse = null;
// 类型转化
try {
parse = sdf.parse(source);
setValue(parse);
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* @param source
* 传递来的日期格式的字符串
*
*/
private SimpleDateFormat getDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat();
// 判断
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {
sdf = new SimpleDateFormat("yyyy-MM-dd");
} else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {
sdf = new SimpleDateFormat("yyyy/MM/dd");
} else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {
sdf = new SimpleDateFormat("yyyyMMdd");
} else {
/**
* 都不匹配了 就让它抛出 TypeMismatchException异常 public
* TypeMismatchException(Object value, Class<?> requiredType) {
* vallue 值能对应requiredType 类型 就不会出现异常 我们就得写一个不能转换的
*/
throw new TypeMismatchException("", Date.class);
}
return sdf;
}
}
自我感觉:Editor代码量比Controller少,但还是都记不住。。。
我去魂斗罗了哈哈哈。。。
仔细看了一下,纠正一下,并不是Controller代码量多,在页面设置了回显,所以代码多了。但是还是不太明白,return、date date=null,date parse=null。下午讨论以后,在写感受。
spring自动类型转换========Converter和PropertyEditor的更多相关文章
- 2. Spring早期类型转换,基于PropertyEditor实现
青年时种下什么,老年时就收获什么.关注公众号[BAT的乌托邦],有Spring技术栈.MyBatis.JVM.中间件等小而美的原创专栏供以免费学习.分享.成长,拒绝浅尝辄止.本文已被 https:// ...
- Spring ConversionService 类型转换(一)Converter
Spring ConversionService 类型转换(一)Converter Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.h ...
- 转:SpringMVC之类型转换Converter(GenericConverter)
转: http://blog.csdn.net/fsp88927/article/details/37692215 SpringMVC 之类型转换 Converter 1.1 目录 1.1 目录 1. ...
- SpringMVC 之类型转换Converter详解转载
SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1 目录 1.1 目录 1.2 ...
- SpringMVC 之类型转换Converter 源代码分析
SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...
- Spring ConversionService 类型转换(二) ConversionService
Spring ConversionService 类型转换(二) ConversionService Spring 系列目录(https://www.cnblogs.com/binarylei/p/1 ...
- BeanUtils中的自动类型转换(二)
javabean package entity; import java.util.Date; /** * 一个测试用: * student,javaBean * @author mzy * 一个标准 ...
- JavaScript系列文章:自动类型转换-续
在上一篇文章中,我们详细讲解了JavaScript中的自动类型转换,由于篇幅限制,没能覆盖到所有的转换规则,这次准备详细讲解一下. 上次我们提到了对象类型参与运算时转换规则: 1). 在逻辑环境中执行 ...
- JavaScript系列文章:自动类型转换
我们都知道,JavaScript是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...
随机推荐
- 关于slavetable
slavetable有两种情况, 从表有三个要素 1.自己主键字段的idfield 2.对应主表的主键字段masterIdField 3.对应主表主键的值 模式一.MasetrEdit模式 也就是主 ...
- LCS poj1080
题目链接:https://vjudge.net/problem/POJ-1080 参考博客:https://yq.aliyun.com/ziliao/372259 题意:给两个字符串,只含有'A',' ...
- Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等
Selenium 如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...
- Mac 动态库加载不上
OC xcode can't found xxx.dylib 1 targer- build phase :link binary with library添加动态库 注意不要将后边的选项选成opti ...
- Ansible Playbook 详解
一.playbook 的简单使用 1.创建文件实例 (1)编辑配置文件 [root@tiejiangSRC1 ~]# cd /etc/ansible/ [root@tiejiangSRC1 ansib ...
- node.js中module模块的理解
node.js中使用CommonJS规范实现模块功能,一个单独的文件就是一个单独的模块.通过require方法实现模块间的依赖管理. 通过require加载模块,是同步操作. 加载流程如下: 1.找到 ...
- [z]spring boot gradle build
I had the same problem. I believe it is caused by the JRE that gradle is configured to use rather th ...
- Android TV开发 焦点控制
转载:http://www.eoeandroid.com/thread-264177-1-1.html 最近在做一款基于Android的互联网电视客户端,开发与phone/pad差不多,但是有一个值得 ...
- js分割数字
var str = "123"; var b = String(str).split(''); 打印b[0].b[1].b[2]看效果...
- hdu (欧拉函数+容斥原理) GCD
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...