解决方案 »

  1.   

    一、首先需要做一个属性编辑器,将字符串转化为Date型,以解决出错的根本原因。
    做法是: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>
    大致思路是这样,不过笔者没有对上述代码测试。
      

  2.   


    谢谢朋友,初始化绑定我分开试了还是存在问题  在properties文件里加上了
    typeMismatch.java.util.Date=请输入正确的日期格式,如2015-1-1
    成功显示了错误信息 ~