springmvc date


- ConversionService:只有数据转换功能;
- ConversionServiceFactoryBean:与ConversionService对应;
- FormattingConversionService:具有数据转换和数据格式化功能;
- FormattingConversionServiceFactoryBean:与FormattingConversionService对应;可以注册自定义的转换器,又可以注册自定义的注解驱动器逻辑。
2. 具体实现
步骤1:配置FormattingConversionServiceFactoryBean

<mvc:annotation-driven conversion-service="conversionService"/><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- <bean class="com.ll.model.StringToPersonConverter" /> --></list></property></bean>
步骤2:使用@DateTimeFormat和@NumberFormat注解对象属性

步骤3:控制层

步骤4:前台请求



3. 简单介绍@DateTimeFormat与@NumberFormat



一、后台日期类型解析到前端
1.在springmvc的配置文件中添加这个.annotation-driven在配置文件中只配置一次 (此方法全局作用)
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
2.直接在字段上写上
@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT-8")
private Date inputtime;
二、前端日期类型传到后台
1.在Controller类中添加这个方法 (此方法全局作用)
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
2.直接在字段上添加
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date beginDate;
第三种:https://blog.csdn.net/u012410733/article/details/72773095
springmvc date的更多相关文章
- springmvc Date类型转换
有时候我们会碰到这么一个问题,有一个实体类,里面有一个Date类型的数据,jsp页面传递的时间参数是String的,这就导致无法对应,springmvc无法帮我们自动封装参数到实体类中了,这里我解决的 ...
- SpringMvc date数据传递处理
1.form表单提交date数据 form表单提交的date数据要在接收的字段上加入@DateTimeFormat注解 @DateTimeFormat(pattern = "yyyy-MM- ...
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
- SpringMVC在传递date型数据时的配置
查阅了好多资料, 最后才发现原来只是添加一个方法就能解决的问题, 但是看了半天又没看明白, 只是知道这么写就能成功, 先记下来, 以后再研究吧, 在配置好springMVC的时候, 可以在前台的for ...
- SpringMVC处理Date类型的成员变量方法
原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...
- SpringMVC解决@ResponseBody返回Json的Date日期类型的转换问题
在做项目的时候,发现后台把Date类型的属性以json字符串的形式返回,前台拿不到转换后的日期格式,始终响应回去的都是long类型时间戳. 查阅资料之后找到解决方法: 方法一(在springmvc的x ...
- springMVC返回json数据时date类型数据被转成long类型
在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...
- 解决:springmvc中接收date数据问题
这里提供三种解决方案. 一.局部转换 :只是对当前Controller类有效 springMVC.xml中添加: <bean class="org.springframework.we ...
- SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用
使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下: 方法一 . 在需要日期转换的Con ...
随机推荐
- 最全,可直接用的一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等。
一些正则校验,判断邮箱,手机号码,车牌号,身份证号,网址,账号,密码,ip,去掉html格式,工商税号等. // 判断邮箱 isValid = [text isValidEmail]; // 判断手机 ...
- 部署前准备--使用Mysql之Django Debug Toolbar安装以及配置
python -c "import django ;print(django.__path__);" 查看python的全局配置 vi /usr/local/lib/python3 ...
- angular学习笔记(3)- MVC
angular1学习笔记(3)- MVC --- MVC终极目标 - 模块化和复用 AngularJs的MVC是借助于$scope实现的!!! 神奇的$scope: 1.$scope是一个POJO(P ...
- py3 pymysql
虽然大家可能在python2.x中用习惯了mysqldb,但是在python3.x中已经不支持那个组件了. 取而代之的是: import pymysql 所以,大家pip起来吧.另外,mysql官方出 ...
- 前端工程化系列[05] Yeoman脚手架使用入门
Yeoman是一款流行的前端的脚手架工具. 脚手架工具可以用来快速的自动生成项目的必要文件和基础文件结构.Yeoman使用的内建命令为yo,同时它自己也是一个包管理工具和自动化任务工具,它基于特定的模 ...
- Unity2017灯光烘焙知识点
去研究一下灯光探针,性能可以提升不少.
- java.lang.NumberFormatException: multiple points错误问题
最近项目一直会出现时间转换报错,一直不知道是什么问题??? java.lang.NumberFormatException: multiple points at sun.misc.Float ...
- too much recursion(太多递归)Uncaught RangeError: Maximum call stack size exceeded BootstrapValidator报错
在BootstrapValidator中已默认遵守Bootstrap规则,form里的每个输入项目必需包含在类为form-group的标签里,否则BootstrapValidator中定义的field ...
- SharePoint每日小贴士Web部件
SharePoint每日小贴士Web部件 项目描写叙述 此Web部件从指定SP自己定义列表或一个选定的 RSS源选择一个随机项目.并显示一张图片.标题和一个Tip. 适 ...
- redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error(转)
redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error windows下安装Redis第一次启动报 ...