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怎么创 ...
随机推荐
- This operation is not available unless admin mode is enabled: FLUSHDB
报错: This operation is not available unless admin mode is enabled: FLUSHDB 参考内容: https://www.cnblogs ...
- 修改Eclipse jdk环境
原因:由于项目原因,要将原有的工程从jdk1.6迁移到jdk1.7 问题:Eclipse默认的jdk环境为jdk1.6 解决方法: 1)首先是安装jdk1.7,以及配置环境变量,在这里就不再说了 2) ...
- VS2015编译Boost1.64
一.下载并解压:boost1.64.0:http://www.boost.org/users/history/version_1_64_0.html 二.以管理员权限运行VS2015命令行工具 三.c ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- 十分有趣的this指向题
var num=5;//9 =>++this.num=>10 var obj={ num:3, fn:function() { num = 9;//window.num=9 (functi ...
- 什么是 JWT -- JSON WEB TOKEN
看链接:http://www.jianshu.com/p/576dbf44b2ae 来个工具类: import java.io.Serializable; import java.util.Date; ...
- lazy初始化和线程安全的单例模式
1.双检锁/双重校验锁(DCL,即 double-checked locking) JDK 版本:JDK1.5 起 是否 Lazy 初始化:是 是否多线程安全:是 实现难度:较复杂 描述:这种方式采用 ...
- Mathematik
Ausdruck auf Deutsch Lösen Problem der Abteilung. 求导. Die Abteilung von 3x ist 3. 3x的导数是3 Lösen Prob ...
- java自学-编程入门
java语言写的代码需要先编译为可执行文件,才能被jvm执行.在下载的jdk安装目录下的bin目录,有两个可执行程序java.exe和javac.exe,javac就是用来编译的,java是执行编译后 ...
- groovy动态类型--能力式设计
动态类型 动态类型中的类型是在运行时推断的,方法及其参数也是在运行时检查的. 能力式设计 被称作鸭子模式:他有这么一个观点:如果它走路像鸭子,叫起来也像鸭子,那么他就是一只鸭子. 契约式设计 相当于J ...