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是类型松散型语言,在声明一个变量时,我们是无法明确声明其类型的,变量的类型是根据其实际值来决定的,而且在运行期间,我们可以随时改变这个变量的值和类型,另外,变量在运行期 ...
随机推荐
- Android笔记:ContextMenu
ContextMenu,称为上下文菜单,也就是长按界面不放,弹出的菜单.使用ContextMenu有三个步骤: (1)调用registerForContextMenu()方法,为视图注册上下文菜单: ...
- DES算法实现(C++版)
#include "memory.h" #include "stdio.h" enum {encrypt,decrypt};//ENCRYPT:加密,DECRY ...
- MysqlMd5加密
MD5加密成功
- open suse linux 磁盘分区
在opensuse 中我是这样对磁盘进行配置的 先添加一块磁盘任意大小 reboot 重启 ls /dev/ | grep sd 可以看到有一块sdb 的磁盘没有分区 fdisk /dev/sdb n ...
- AVL树与红黑树(R-B树)的区别与联系
AVL树(http://baike.baidu.com/view/593144.htm?fr=aladdin),又称(严格)高度平衡的二叉搜索树.其他的平衡树还有:红黑树.Treap.伸展树.SBT. ...
- vue axios请求/响应拦截器
// main.js中配置 // 引入 axios import Axios from 'axios' // 这时候如果在其它的组件中,是无法使用 axios 命令的. // 但如果将 axios 改 ...
- 自动化测试之selenium工具简单介绍
一.selenium简单介绍 1.selenium的成员 2.selenium工作原理 二.webdrive 常见元素定位
- redis 集群java.lang.NoSuchMethodError:SpringJAR包版本冲突错误解决方法
项目中出现如下错误,记录下解决方法: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exce ...
- 获取URL某个参数
/* 获取URL某个参数(可以是中文) * 返回:字符串 */ function getUrlParam(key) { // 获取参数 var url = window.location.search ...
- iconfont项目成员添加不进去的问题
经别的网友提醒,发现是我用的chrome浏览器的问题,这顿折腾....解决方案:换一个浏览器试试.