主要有两步,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. node js linux / OS 安装

    rm -rf 删除文件夹名字rm -rf 软连接名称 1.安装taryum install -y tar 3. 下载node https://nodejs.org/en/download/ 4. 拷贝 ...

  2. asp.net AD 域验证

    1.获取环境变量 string strAuthUser = request.ServerVariables["AUTH_USER"] 以上这行代码是用来获取当前用户的登录名 2.I ...

  3. Asp.net获取系统信息

    [DllImport("kernel32")]        public static extern void GlobalMemoryStatus(ref MEMORY_INF ...

  4. 解决报错:import sun.misc.BASE64Decoder无法找到

    解决报错:import sun.misc.BASE64Decoder无法找到 2017年09月29日 16:03:26 chaoyu168 阅读数:2116 标签: sun.misc.BASE64De ...

  5. python 格式化时间含中文报错: 'locale' codec can't encode character '\u5e74'

    执行下面代码报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal ...

  6. Java学习笔记day03_引用数据类型

    1.引用数据类型 步骤: 1. 导包   2. 创建引用类型变量 类型 变量名 = new 类型名();   3. 使用数据类型的功能 变量名.功能名(); 如Scanner类: import jav ...

  7. js 实现继承的几种方式

    //js中实现继承的几种方式 //实现继承首先要有一个父类,先创造一个动物的父类 function Animal(name){ this.name = name; this.shoot = funct ...

  8. 27-----BBS论坛

    BBS论坛(二十七) 27.首页帖子列表布局完成 (1)apps/models.py 把帖子跟用户关联起来 class PostModel(db.Model): __tablename__ = 'po ...

  9. APP测试总结1

    1.安装.卸载测试 安装.卸载测试主要针对编译后源程序生成的APK安装文件 主要测试点: 1).生成的APK文件在真机上可以安装及下载 2).Android手机端的通用安装工具,如:豌豆荚及91助手等 ...

  10. Android 中怎么重新启动APP或系统

    重新启动应用程序,有两种方法,分别是: 1.通过ActivityManager来重新启动应用程序: ActivityManager manager = (ActivityManager)this.ge ...