SpringMVC 对 Date 类型转换

1. 在 springmvc.xml 中配置,代码中不需要做任何修改
1.1 必须额外导入joda-time.jar
1.2 时间类型 java.sql.Date

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 扫描注解 --><context:component-scan base-package="com.bjsxt.controller"></context:component-scan><!-- 注解驱动 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="registerDefaultFormatters" value="false" /><property name="formatters"><set><beanclass="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /></set></property><property name="formatterRegistrars"><set><beanclass="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"><property name="dateFormatter"><beanclass="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"><property name="pattern" value="yyyy-MM-dd" /></bean></property></bean></set></property></bean><!-- 静态资源 --><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources><mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
</beans>

2. 使用注解. 在需要转换的参数或实体类属性上添加
@DateTimeFormatter(pattern=”表达式”)
2.1 使用 Date 参数接收

@Controller
public class DemoController {@RequestMapping("demo")public String demo(DemoDate demo) {System.out.println("In DemoController, demo = " + demo);return "abc.jsp";}
}

2.2 在pojo实体类中

public class DemoDate {@DateTimeFormat(pattern="yyyy/MM/dd")private Date time;

2.3 注意地方:
2.3.1 不需要导入额外jar
2.3.2 Datejava.util.Date

【SpringMVC】SpringMVC 对 Date 类型转换相关推荐

  1. SpringMVC注解@initbinder解决类型转换问题

    SpringMVC注解@initbinder解决类型转换问题 参考文章: (1)SpringMVC注解@initbinder解决类型转换问题 (2)https://www.cnblogs.com/an ...

  2. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --><context:annotation- ...

  3. 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型

    将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型 问题描述: java里面时间类型转换成json数据就成这样了: "cr ...

  4. string转换成date类型_你竟然知道SpringMVC是如何完成类型转换和数据绑定的吗?...

    # 前言 SpringMVC是目前主流的Web MVC框架之一. public String method(Integer num, Date birth) { ...} Http请求传递的数据都是字 ...

  5. springmvc中Date类型转换

    //实现类型转换接口 package cn.bdqn.stusystem.util;import java.text.ParseException; import java.text.SimpleDa ...

  6. SpringMVC注解 @initbinder 解决类型转换问题

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 使用 SpringMVC 时,常遇到表单中日期字符串和 JavaBean 的 Date 类型的转换, ...

  7. 解决:springmvc中接收date数据问题

    springmvc Controller类中需要接收的是Date类型,但是在页面端传过来的是String类型,就会出现以下异常 Failed to convert value of type 'jav ...

  8. initbinder对ajax不起作用,详解SpringMVC注解@initbinder解决类型转换问题

    在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题. ...

  9. springmvc框架后台Date类型字段接收数据

    在springmvc框架里有的时候是实体类接收的数据里面有特殊类型例如Date类型 直接放到你自己Controller层就可以(这样date类型的字段就能接收到数据) @InitBinderprote ...

最新文章

  1. mac 部署python环境
  2. 美创企推出仅90斤的物流无人车,成本低至5000美元
  3. JSP知识点笔记-常用技术方法
  4. 内存管理单元MMU学习
  5. 【转】AngularJs 弹出框 model(模态框)
  6. 真香!原来 CLI 开发可以这么简单
  7. DEFINE_PER_CPU
  8. 最详细的Fast RCNN论文笔记
  9. vue ---- filter(vue2才有)
  10. ASP.NET AjaxPro的应用 .AjaxPro使用中“XXX未定义”的一种解决方法(转载的)
  11. Command Prompt + Perl 脚本编写笔记
  12. JavaScript 对象 和 函数
  13. python 下载及安装-CentOS下python的下载及安装
  14. .NET开源项目介绍及资源推荐:单元测试
  15. 基础知识 字节、KB、MB、GB 之间的换算关系
  16. 为了对电脑进行保护,已经阻止此应用 解决办法
  17. Unity游戏开发入门
  18. python3 + opencv +pyzbar实时检测二维码 / 定位二维码,并绘制出二维码的框和提取二维码内容
  19. 硬件电路设计之电源电路的设计
  20. (转载)如何写论文之标题篇

热门文章

  1. CH - 0501 货仓选址(中位数)
  2. POJ - 3565 Ants(二分图最小权匹配+KM+思维)
  3. rockbox主题包安装_DUX主题
  4. kafka 削峰_从面试角度一文学完 Kafka
  5. CSTrackV2解读
  6. 安卓进阶系列-08异步加载AsynsTask使用
  7. Timus1286(欧几里德算法的应用)
  8. windows终止处理程序( __try __finally) 简单解析
  9. Windows系统程序设计之结构化异常处理
  10. C++ 泛型编程(一):模板基础:函数模板,类模板,模板原理,模板匹配规则