配置spring的配置文件:

 <bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="app06a.converter.StringToDateConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>

这样配置好了之后,就可以创建一个Converter的装换器类来写一个转换器的实现。这里我们写的转换器是把String类型转换为Date类型的。日期格式为value后面的。

 public class StringToDateConverter implements Converter<String,Date>{
private String datePattern;
public StringToDateConverter(String datePattern){
this.datePattern=datePattern;
System.out.println("instiating"+datePattern);
}
@Override
public Date convert(String s) {
SimpleDateFormat dateformat=new SimpleDateFormat();
dateformat.setLenient(false);
try {
return dateformat.parse(s);
} catch (ParseException e) {
throw new IllegalArgumentException("invailed date"+datePattern+"\"");
}
}
}

然后实现了Converer<S,T>接口。

这样,若是日期输入错误,则会返回之前输入的页面,页面上也会出现提示:

 @RequestMapping(value="/employee_input")
public String inputEmployee(Model model){
model.addAttribute("employee",new Employee());
return "EmployeeForm";
} @RequestMapping(value="/employee_save")
public String saveEmployee(@ModelAttribute Employee employee,BindingResult bindresult,Model model){
if(bindresult.hasErrors()){
FieldError fielderror=bindresult.getFieldError();
return "EmployeeForm";
}
model.addAttribute("employee",employee);
return "EmployeeDetails";
}

==========================================================

Spring中的转换器:Converter的更多相关文章

  1. Spring中使用JMS

    JMS为了Java开发人员与消息代理(message broker)交互和收发消息提供了一套标准API.而且,由于每个message broker都支持JMS,所以我们就不需要学习额外的消息API了. ...

  2. Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)

    1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...

  3. 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor

    #### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...

  4. 【小家Spring】聊聊Spring中的数据绑定 --- DataBinder本尊(源码分析)

    每篇一句 唯有热爱和坚持,才能让你在程序人生中屹立不倒,切忌跟风什么语言或就学什么去~ 相关阅读 [小家Spring]聊聊Spring中的数据绑定 --- 属性访问器PropertyAccessor和 ...

  5. Spring官网阅读(十六)Spring中的数据绑定

    文章目录 DataBinder UML类图 使用示例 源码分析 bind方法 doBind方法 applyPropertyValues方法 获取一个属性访问器 通过属性访问器直接set属性值 1.se ...

  6. Spring官网阅读(十五)Spring中的格式化(Formatter)

    文章目录 Formatter 接口定义 继承树 注解驱动的格式化 AnnotationFormatterFactory FormatterRegistry 接口定义 UML类图 FormattingC ...

  7. Spring官网阅读(十四)Spring中的BeanWrapper及类型转换

    文章目录 接口定义 继承关系 接口功能 1.PropertyEditorRegistry(属性编辑器注册器) 接口定义 PropertyEditor 概念 Spring中对PropertyEditor ...

  8. 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...

  9. 这篇文章,我们来谈一谈Spring中的属性注入

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...

随机推荐

  1. Iviews视频搜索引擎

    随着视频类型的增加和数据量的日益庞大,如何有效地组织和管理这些数据,使人们能够方便地从大量视频数据中找到自己感兴趣的相关视频片段已成为一种迫切的需求,而能够满足这一需求的技术便是目前人们普遍关注的基于 ...

  2. mongo 固定集合,大文件存储,简单优化 + 三招解决MongoDB的磁盘IO问题

    1.固定集合 > db.createCollection(, max:});//固定集合 必须 显式创建. 设置capped为true, 集合总大小xxx字节, [集合中json个数max] { ...

  3. EasyMock set方法报错: java.lang.AssertionError

    有返回值的方法没问题, 直接andReturn就行了. EasyMock.expect(info.getWebTitle()).andReturn(StringUtils.EMPTY).anyTime ...

  4. 实现Date函数属性中的format方法

    js中没有Date.format方法的,所以在date属性中加format方法 //js格式化属性 Date.prototype.format = function (format) { var o ...

  5. PHP之string之addslashes()函数使用

    addslashes (PHP 4, PHP 5, PHP 7) addslashes - Quote string with slashes addslashes - 使用反斜线引用字符串 Desc ...

  6. hadoop ——HDFS存储

    一.HDFS概念 二.HDFS优缺点 三.HDFS如何存储 一.HDFS概念 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据 ...

  7. Mysql日期类型大小比较---拉取给定时间段的记录

    我们知道,mysql里边,日期类型有很多表现形式,date, datetime,timestamp等类型.考虑这样一种场景: 按时间段拉取给定时间段的内容,这时,我们就得使用日期类型的比较了. 表结构 ...

  8. ReferenceError: “alert” is not defined

    用Node.js单独运行js文件时,其中定义的alert不可用 alert is not part of JavaScript, it's part of the window object prov ...

  9. 【OpenCV】邻域滤波:方框、高斯、中值、双边滤波

    原文:http://blog.csdn.net/xiaowei_cqu/article/details/7785365 邻域滤波(卷积)   邻域算子值利用给定像素周围像素的值决定此像素的最终输出.如 ...

  10. 使用 OLEDB 及 SqlBulkCopy 将多个不在同一文件夹下的 ACCESS mdb 数据文件导入MSSQL

    注:转载请标明文章原始出处及作者信息http://www.cnblogs.com/z-huifei/p/7380388.html 前言 OLE DB 是微软的战略性的通向不同的数据源的低级应用程序接口 ...