SpringMVC日期类型转换问题三大处理方法归纳
转自:http://blog.csdn.net/chenleixing/article/details/45190371
方法1.在实体中加入日期格式化注解
@DateTimeFormat(pattern="yyyy-MM-dd") private Date birthday;
方法2.在controller中加入数据绑定代码
package com.fyh.www.pojo.user;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.annotation.InitBinder;public class LoginController {@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值} }
方法3.注册一个全局日期类型转化器
注册全局转化器
<mvc:annotation-driven conversion-service="conversionService"/><!-- 设置Converter转换器 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><!-- 设置多个转换器 --><property name="converters"><list><bean class="com.fyh.www.common.mvcConverter.CustomTrimConverter"></bean></list></property></bean>
具体的实现代码
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); try { return dateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; }
后台date类型到前台String类型
JSP模版引擎方法:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>
Freemarker模版引擎方法:
<input id="receiveAppTime" name="receiveAppTime" type="text" value="${(bean.receiveAppTime?string('yyyy-MM-dd'))!}" />
转载于:https://www.cnblogs.com/YLQBL/p/8493680.html
SpringMVC日期类型转换问题三大处理方法归纳相关推荐
- SpringMVC日期类型转换问题处理方法归纳
前言 我们在SpringMVC开发中,可能遇到比较多的问题就是前台与后 台实体类之间日期转换处理的问题了,说问题也不大,但很多人开发中经常会遇到这个问题,有时很令人头疼,有时间问题暴露的不是很明显,然 ...
- SpringMVC 日期类型转换
全局日期类型转换配置 配置SpringMVC配置文件 <bean id="conversionService" class="org.springframework ...
- SpringMVC 之类型转换Converter 源代码分析
SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...
- 深度盘点:Python 变量类型转换的 6 种方法
大家好,今天我来给大家介绍 Python 变量类型转换的 6 种方法.梳理不易,喜欢记得点赞.收藏.关注. [注]完整版代码.数据.技术交流,文末获取 一.变量类型及转换 对于变量的数据类型而言,Pa ...
- SpringMVC 之类型转换Converter详解转载
SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1 目录 1.1 目录 1.2 ...
- Java进阶学习第十六天(Struts框架:配置、数据处理、自动封装、日期类型转换、文件上传)
一.Struts2配置 1.Struts2的Action开发的几种方式 ① 方式1:继承ActionSupport(一般使用方法) public class UserAction extends Ac ...
- springboot springmvc 抛出全局异常解决方法
springboot springmvc 抛出全局异常解决方法 参考文章: (1)springboot springmvc 抛出全局异常解决方法 (2)https://www.cnblogs.com/ ...
- java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)...
转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...
- 分数化成有限小数的方法_[153]各种循环小数化成分数的方法归纳
各种循环小数化成分数的方法归纳 一.纯循环小数化分数 从小数点后面第一位就循环的小数叫做纯循环小数.怎样把它化为分数呢?看下面例题. 例1把纯循环小数化分数: 从以上例题可以看出,纯循环小数的小数部分 ...
最新文章
- Google Test(GTest)使用方法和源码解析——概况
- java中拼写xml
- openshift学习_在OpenShift上将JMS与JBoss A-MQ结合使用。 学习了有关远程客户端和加密的经验。...
- 内置函数之sorted,filter,map
- Solr Facet技术的应用与研究
- 如何在 5 分钟内读懂区块链的架构思维?
- vb升级工作笔记001---VB.NET升级到VB.NET 随时更新
- edge浏览器识别ip地址为手机号的解决办法
- 《Linux内核设计与实现》读书笔记 - 目录 (完结)【转】
- 联想用u盘重装系统步骤_保姆级重装系统教程在此
- angularJs解决跨域问题-最简单的完美实例
- case when then end.sql
- 地图poi数据库下载
- GJB438C-2021规范详解其二
- 游戏测试用例及游戏测试bug详解
- C4D预设如何安装?
- 今生梦一场,思念你的殇
- requests使用cookie模拟登陆豆瓣
- 妇科癌症中的肠道菌群:病因、治疗潜力
- 还在花钱抢票?12306 已屏蔽60款抢票软件!