您的位置:首页 > 编程语言 > Java开发

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

2017-04-20 14:07 288 查看
我们从前台想后天传入日期的 时候会发现报了一个天大的错,那是因为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 这就完成了配置了 在输入和上面对应的格式就不会错了 格式可以自己任意选择都是没问题的哦

以上有什么问题请各位大牛及时提出,小弟感激不尽
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: