SpringMVC 传日期参数到后台的实例讲解
作者:tianxia_09 时间:2022-01-18 23:31:48
1、注解方式,在controller层通过initBinder注解实现
@InitBinder
public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws Exception {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
binder.registerCustomEditor(Date.class, dateEditor);
}
2、类型转换,SpringMvc提供了Converter接口
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String stringDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
spring.xml中配置转换器
<!-- 第一步: 创建自定义日期转换规则 -->
<bean id="dateConvert" class="xxx.xxx.DateConvert"/>
<!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConvert"/>
</set>
</property>
</bean>
<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionService"/>
来源:http://www.cnblogs.com/tianxia-09/p/8032673.html
标签:SpringMVC,日期,参数,后台
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析C# 函数的传值与传址
2023-11-22 04:46:57
![](https://img.aspxhome.com/file/2023/0/71730_0s.png)
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
![](https://img.aspxhome.com/file/2023/0/61030_0s.png)
AQS加锁机制Synchronized相似点详解
2023-08-04 22:36:55
![](https://img.aspxhome.com/file/2023/8/57918_0s.png)
C#中使用split分割字符串的几种方法小结
2023-06-18 17:23:21
Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码
2023-11-23 08:23:43
![](https://img.aspxhome.com/file/2023/5/59865_0s.png)
基于Idea+Jconsole实现线程监控步骤
2021-07-29 10:39:40
![](https://img.aspxhome.com/file/2023/4/61824_0s.png)
Java中SpringSecurity密码错误5次锁定用户的实现方法
2021-07-10 10:38:17
Go Java算法猜数字游戏示例详解
2022-03-02 21:01:50
Unity UGUI实现简单拖拽图片功能
2022-11-26 19:55:03
![](https://img.aspxhome.com/file/2023/9/77289_0s.jpg)
Unity OnGUI实时显示游戏FPS
2021-10-05 22:53:39
SpringBoot2 参数管理实践之入参出参与校验的方式
2022-12-25 02:44:30
实例解析使用Java实现基本的音频播放器的编写要点
2023-11-29 14:54:26
springmvc 参数绑定总结
2023-11-16 21:30:44
Mybatis如何传入多个参数的实现代码
2022-02-26 02:52:24
Java实战之基于swing的QQ邮件收发功能实现
2023-11-15 01:34:26
Mybatis中连接查询和嵌套查询实例代码
2021-08-23 13:24:31
SSH框架网上商城项目第2战之基本增删查改、Service和Action的抽取
2023-06-21 19:16:23
![](https://img.aspxhome.com/file/2023/4/57494_0s.png)
简单了解springboot的jar包部署步骤
2021-07-02 14:22:48
Java Swing中JDialog实现用户登陆UI示例
2021-10-12 13:58:00
![](https://img.aspxhome.com/file/2023/9/68709_0s.jpg)
剖析Java中阻塞队列的实现原理及应用场景
2023-09-01 17:33:07