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是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...
随机推荐
- hibernate中调用query.list()而出现的黄色警告线
使用hibernate的时候会用到hql语句查询数据库, 那就一定会用到query.list();这个方法, 那就一定会出现一个长长的黄色的警告线, 不管你想尽什么办法, 总是存在, 虽然说这个黄色的 ...
- Session和Cookie的理解
原文地址:https://juejin.im/post/5aede266f265da0ba266e0ef
- Django项目的创建与管理和pycharm与Github的秘密
随笔 - 174 文章 - 21 评论 - 19 Django项目创建与管理 1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...
- 最小生成树 prime算法 UVALive - 6437
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...
- 最小k度限制生成树
[题目描述] 给你一个图,n个点,m条边,求一颗生成树满足如下条件: (1)结点1的度不超过k. (2)在(1)条件下所求生成树最小. [算法引入] 最小k度限制生成树,就是指有特殊的某一点的度不能超 ...
- 优化-最小化损失函数的三种主要方法:梯度下降(BGD)、随机梯度下降(SGD)、mini-batch SGD
优化函数 损失函数 BGD 我们平时说的梯度现将也叫做最速梯度下降,也叫做批量梯度下降(Batch Gradient Descent). 对目标(损失)函数求导 沿导数相反方向移动参数 在梯度下降中, ...
- 《基于Nginx的中间件架构》学习笔记---1.环境配置
一.环境调试确认 (四项确认) 1.确认系统网络 ping www.baidu.com 2.确认yum可用 yum list|grep gcc 3.确认关闭iptables规则 iptables -L ...
- WIN7安装jdk1.7
@官网下载地址 我的64位的 下载后双击,一直下一步就行.出现下面这个关掉就行 新建环境变量 JAVA_HOME C:\Program Files\Java\jdk1.7.0_04 新建环境变量CLA ...
- Repeater绑定数据 后台自定义的DataTable
前台:<asp:Repeater ID="Repeater1" runat="server"> <ItemTemplat ...
- mysql的事务,隔离级别和锁
事务就是一组一起成功或一起失败的sql语句.事务还应该具备,原子性,一致性,隔离性和持久性. 一.事务的基本要素 (ACID) 1.原子性:事务开始后,所有的操作,要么全部成功,要么全部失败,不可能处 ...