spring数据验证格式化错误 spring国际化数据格式化数据验证 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一、首先需要做一个属性编辑器,将字符串转化为Date型,以解决出错的根本原因。做法是:public class DateEditor extends PropertyEditorSupport { //重写setAsText方法,将字符串转换成Date类型 public void setAsText(String text) throws IllegalArgumentException { } //重写getAsText方法,将Date类型转换成字符串,当Form表单提交出错后,可以回显 public String getAsText(){ }}//装配转换器,在Controller类增加一个initBinder方法,这样所有日期型字段则自动调用此编辑器进行转换:@InitBinderpublic void initBinder(WebDataBinder binder, WebRequest request) { binder.registerCustomEditor(Date.class, new DateEditor("yyyy-MM-dd", true));}//楼主配置了@DateTimeFormat,以及现在Spring 4高版本是不是自动解决这了些问题,楼主可以研究下。二、友好地提示出错信息:1、在classes下创建一个messages.properties配置文件,内容为:typeMismatch.java.util.Date=请输入正确的日期格式,如2015-1-1 2、将资源文件交给Spring容器<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="WEB-INF/classes/messages" /></bean>大致思路是这样,不过笔者没有对上述代码测试。 谢谢朋友,初始化绑定我分开试了还是存在问题 在properties文件里加上了typeMismatch.java.util.Date=请输入正确的日期格式,如2015-1-1成功显示了错误信息 ~ Java 一个简单问题 求大侠回答 getHibernateTemplate()和new HibernateTemplate()区别 xdoclet模板文件怎么引用别的文件? 跪求一个分页以及调用方法 怎么在jsp页面里不让字体岁页面大小改变 请教shark初始化配置及载入xpdl代码 Lomboz 问题 急! 一道关于J2EE的选择题,求解? 为什么已经将要用到的org.jdom加入类库区还是编译时说不存在? 分隔XML文件? mybatis分页问题,请教各位大牛们 eclipse运行的是哪个类里的主函数?
做法是:public class DateEditor extends PropertyEditorSupport {
//重写setAsText方法,将字符串转换成Date类型
public void setAsText(String text) throws IllegalArgumentException {
}
//重写getAsText方法,将Date类型转换成字符串,当Form表单提交出错后,可以回显
public String getAsText(){ }
}//装配转换器,在Controller类增加一个initBinder方法,这样所有日期型字段则自动调用此编辑器进行转换:
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new DateEditor("yyyy-MM-dd", true));
}
//楼主配置了@DateTimeFormat,以及现在Spring 4高版本是不是自动解决这了些问题,楼主可以研究下。
二、友好地提示出错信息:
1、在classes下创建一个messages.properties配置文件,内容为:typeMismatch.java.util.Date=请输入正确的日期格式,如2015-1-1
2、将资源文件交给Spring容器<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/messages" />
</bean>
大致思路是这样,不过笔者没有对上述代码测试。
谢谢朋友,初始化绑定我分开试了还是存在问题 在properties文件里加上了
typeMismatch.java.util.Date=请输入正确的日期格式,如2015-1-1
成功显示了错误信息 ~