主要有两步,controller中添加initBinder方法,再创建一个时间类型数据转换类就OK了。

1、在Controller中创建方法:

// 相关包
import java.text.DateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder; // 方法
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
DateFormat df = new MyDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}

例如:

@Controller
public class MyController {
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
DateFormat df = new MyDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
// 省略
}

2、创建日期类:MyDateFormat

package com.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class MyDateFormat extends SimpleDateFormat { private static final long serialVersionUID = 2371168145105228746L; private String[] patterns = new String[] { "yyyyMMdd", "yyyy-MM-dd HH:mm", "HH:mm", "yyyy-MM-dd" }; public Date parse(String source) throws ParseException {
try {
if (source == null)
source = "";
if (toPattern().length() == source.length())
return super.parse(source);
for (int i = 0; i < patterns.length; i++) {
if (patterns[i].length() == source.length()) {
applyPattern(patterns[i]);
return super.parse(source);
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public MyDateFormat() {
super();
} public MyDateFormat(String pattern) {
super(pattern);
} }

come on ,欢迎讨论;

Spring MVC框架下提交Date数据无法在controller直接接收的更多相关文章

  1. Spring MVC框架下的第一个Hello World程序

    本程序是一个maven程序,使用maven方便管理jar包和程序,简化了操作步骤.本程序的目的是通过一个简单的程序,了解Spring MVC框架的基本工作流程,由简入繁的学习Spring MVC框架, ...

  2. Spring MVC表单提交中文数据出现乱码

    http://jiessiedyh.iteye.com/blog/475541 http://bigcat.easymorse.com/?p=474 Spring MVC 教程,快速入门,深入分析 h ...

  3. Spring mvc框架下使用kaptcha生成验证码

    1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...

  4. Spring MVC框架下 将数据库内容前台页面显示完整版【获取数据库人员参与的事件列表】

    1.书写jsp页面包括要显示的内容[people.jsp] <!-- 此处包括三个方面内容: 1.包含 文本输入框 查询按钮  查询结果显示位置 (paging) 2.包括对按钮(button) ...

  5. Spring MVC框架下 从后台读取数据库并显示在前台页面【笔记自用 不推荐作为参考】

    1.书写jsp页面  people.jsp 1.设计显示格式以及内容显示 2.设计显示内容的范围 2.书写entity实体类 PeopleFormMap.java 书写传入的参数主要包括 要引用的数据 ...

  6. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  7. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  8. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  9. spring MVC框架入门(外加SSM整合)

    spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...

随机推荐

  1. c++中STL中的next_permutation函数基本用法

    对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...

  2. 毕业设计 python opencv实现车牌识别 矩形矫正

    主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...

  3. 2.Servlet(一)

    1.Servlet的编写.访问过程: (1)编写部署Servlet程序: 编写源文件->编译类文件->部署程序->运行->Servlet处理请求,返回响应. (2)Eclips ...

  4. 转 ORACLE 查看RMAN的备份信息总结

    http://www.cnblogs.com/kerrycode/p/5684768.html 关于Oracle数据库的RMAN备份,除了邮件外,是否能通过其它方式检查RMAN备份的成功与失败呢?其实 ...

  5. git使用标准

    git 使用规范 团队开发中,要遵循一个合理.清晰的git使用流程非常重要的.否则每个人提交一堆杂乱我长的commit,项目很快就变得难以协调和维护 第一步:创建新分支 首先,每一次开发新功能,都应该 ...

  6. python3与python2的区别 记录一波

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...

  7. 基于MVC模式开发的后台框架

    1.ThinkCMF 2.NFine快速开发平台 3.力软快速开发框架 如有好的开发框架希望可以一起交流

  8. python_文件操作代码实例

    """提示:代码中的内容均被注释,请参考,切勿照搬""" 1 #文件的打开和关闭 ''' 文件对象 = open('文件名','使用方式') ...

  9. 大数据 Big Data howto

    The Fourth Paradigm: Data-Intensive Scientific Discovery http://research.microsoft.com/en-us/collabo ...

  10. [转]win系统下nodejs安装及环境配置

    本文转自:http://www.cnblogs.com/linjiqin/p/3765390.html 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...