springmvc配置全局日期转换器(ssm四)

来源:互联网 发布:dota2英雄知乎 编辑:程序博客网 时间:2024/06/05 20:36

我们从前台想后天传入日期的 时候会发现报了一个天大的错,那是因为spring并没有帮我们自行转换日期格式,为了方便开发只能配置一个全局日期转换器 这样就方便了

下面我们就来看下如何配置

1.在我们项目中建立一个包 我通常是建立web包的 在包里面建立DateEnride.java

package com.newfail.core.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

public class DateEnride implements WebBindingInitializer   {

public void initBinder(WebDataBinder binder, WebRequest request) {
//日期转换器
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,true));
}

}

编写好后  我们在spring-mvc.xml中配置如下代码


ok 这就完成了配置了 在输入和上面对应的格式就不会错了 格式可以自己任意选择都是没问题的哦

以上有什么问题请各位大牛及时提出,小弟感激不尽

0 0