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