Spring中的转换器:Converter
配置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的更多相关文章
- Spring中使用JMS
JMS为了Java开发人员与消息代理(message broker)交互和收发消息提供了一套标准API.而且,由于每个message broker都支持JMS,所以我们就不需要学习额外的消息API了. ...
- Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)
1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...
- 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor
#### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...
- 【小家Spring】聊聊Spring中的数据绑定 --- DataBinder本尊(源码分析)
每篇一句 唯有热爱和坚持,才能让你在程序人生中屹立不倒,切忌跟风什么语言或就学什么去~ 相关阅读 [小家Spring]聊聊Spring中的数据绑定 --- 属性访问器PropertyAccessor和 ...
- Spring官网阅读(十六)Spring中的数据绑定
文章目录 DataBinder UML类图 使用示例 源码分析 bind方法 doBind方法 applyPropertyValues方法 获取一个属性访问器 通过属性访问器直接set属性值 1.se ...
- Spring官网阅读(十五)Spring中的格式化(Formatter)
文章目录 Formatter 接口定义 继承树 注解驱动的格式化 AnnotationFormatterFactory FormatterRegistry 接口定义 UML类图 FormattingC ...
- Spring官网阅读(十四)Spring中的BeanWrapper及类型转换
文章目录 接口定义 继承关系 接口功能 1.PropertyEditorRegistry(属性编辑器注册器) 接口定义 PropertyEditor 概念 Spring中对PropertyEditor ...
- 谈谈Spring中的对象跟Bean,你知道Spring怎么创建对象的吗?
本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 推荐阅读: Spring官网阅读 | 总结篇 Spring杂 ...
- 这篇文章,我们来谈一谈Spring中的属性注入
本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...
随机推荐
- c# timer使用
C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Timers.Timer 这三个Timer我想大家对S ...
- Mac下安装eclipse(Mac 10.12/JDK/tomcat)
1.到官网https://www.eclipse.org/downloads/eclipse-packages/下载安装包 2.安装 注意:安装ecllipse时一定要安装JDK先,最新版本的ecli ...
- MySQL设置远程连接服务器
默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件. 一.修改/etc/mysql/my.conf找到bind-address = 127.0 ...
- django notes 五:Writing models
models 其实也没什么好说的,就是普通的 python 类 settings 中配置数据库连接 DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...
- maven项目debug调试不能够进入源码问题解决
Maven项目在debug调试模式的时候,进入调试模式,但是没有进入源码界面. 上述问题的解决方法如下: 第一步: 第二步: 第三步: 第四步: 第五步: 到这里就解决了:
- JavaScript数据结构-11.散列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- *2-3-7-加入field_automation机制
在2.3.3节中引入my_mointor时,在my_transaction中加入了my_print函数: 在2.3.5节中引入reference model时,加入了my_copy函数: 在2.3.6 ...
- ora-01747:因为表中存在关键字造成的
ORCLE报错解决(ora-01747:无效的用户.表.列,表.列) 一.ora-01747:无效的用户.表.列,表.列 这个问题出现是因为表中存在关键字造成的,如果想新增数据直接用sql语句,查询 ...
- iOS 自定义步骤进度条
新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图. 之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做. 1.用进度条做的首先要解决的是进度条的高度问题,可 ...
- 玩转树莓派《二》——用python实现动画与多媒体
环境:树莓派,系统raspbian,系统自带两个版本的python以及pygame. 1.画板 程序如下: !/home/pi/game_1.py import pygame width = 640 ...