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。下午讨论以后,在写感受。

最新文章

  1. [LeetCode] Design Hit Counter 设计点击计数器
  2. SecureCRT 常用命令
  3. [你必须知道的.NET]第三十五回,判断dll是debug还是release,这是个问题
  4. android116 轮播 viewPager实现
  5. SQL Server 行的删除与修改-------------(未完待续P222 deep SQL Server 222 )
  6. Orz 终于有了自己的博客地址
  7. springMVC框架建设进程
  8. IOC and DI
  9. MySQL5.7绿色版(免装版)的初始化和修改密码
  10. DBoW2算法原理介绍
  11. python并发编程之协程知识点
  12. noj算法 素数环 回溯法
  13. Java实现字符串倒序输出的几种方法
  14. 201709011工作日记--Volley源码详解(二)
  15. 创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成S
  16. 机器学习(1):Logistic回归原理及其实现
  17. 简单的搭建php开发平台 WAMP
  18. 抓包工具Charles简单使用介绍
  19. 使用myeclipse开发java,解决java中继承JFrame类出现The type JFrame is not accessible due to restriction的问题
  20. WAF防御能力评测及工具

热门文章

  1. CSS float清除浮动
  2. Django model 中的 class Meta 详解
  3. 第四章&#160;栈与队列(a)栈接口与实现
  4. OSPF网络类型不一致路由无法计算的问题
  5. 项目总结02:百度地图js 基本用法介绍
  6. jsp页面\n换行替换
  7. 基于RBAC权限验证, 中间价middleware实现, views 登录视图代码
  8. BCH/BSV coin split troubleshooting
  9. android--Activity有返回值的跳转
  10. poj 2492(关系并查集) 同性恋