Spring MVC框架下提交Date数据无法在controller直接接收
主要有两步,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直接接收的更多相关文章
- Spring MVC框架下的第一个Hello World程序
本程序是一个maven程序,使用maven方便管理jar包和程序,简化了操作步骤.本程序的目的是通过一个简单的程序,了解Spring MVC框架的基本工作流程,由简入繁的学习Spring MVC框架, ...
- Spring MVC表单提交中文数据出现乱码
http://jiessiedyh.iteye.com/blog/475541 http://bigcat.easymorse.com/?p=474 Spring MVC 教程,快速入门,深入分析 h ...
- Spring mvc框架下使用kaptcha生成验证码
1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...
- Spring MVC框架下 将数据库内容前台页面显示完整版【获取数据库人员参与的事件列表】
1.书写jsp页面包括要显示的内容[people.jsp] <!-- 此处包括三个方面内容: 1.包含 文本输入框 查询按钮 查询结果显示位置 (paging) 2.包括对按钮(button) ...
- Spring MVC框架下 从后台读取数据库并显示在前台页面【笔记自用 不推荐作为参考】
1.书写jsp页面 people.jsp 1.设计显示格式以及内容显示 2.设计显示内容的范围 2.书写entity实体类 PeopleFormMap.java 书写传入的参数主要包括 要引用的数据 ...
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- spring MVC框架入门(外加SSM整合)
spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...
随机推荐
- python解决excel工作薄合并处理(openpyxl处理excel2010以上版本)
前段时间使用xlrd.xlwt对文件进行处理(https://www.cnblogs.com/pinpin/p/10287491.html),但是只能处理excel2010以下版本,所以又写了个处理e ...
- Problem06 求最大公约数及最小公倍数
题目:输入两个正整数m和n,求其最大公约数(m,n)和最小公倍数[m,n]. 程序分析:利用辗转相除法. 利用辗除法:用较大数除以较小数,再用出现的余数(第一余数)去除除数, 再用出现的余数(第二余数 ...
- thinkPHP5.0表单令牌使用
表单令牌的作用:避免表单的重复提交(如在tp5提交成功等待跳转页面刷新页面会在次提交表单) 原理:在初始化表单时,生成一个session标识‘token’,提交表单时将这个token一起提交过去,然后 ...
- vue常见依赖安装
1):$ npm install less less-loader --save 2)style里 <style lang='less'> 2): $ npm i vue-resource ...
- Unable to verify your data submission.加入了_csrf也报400错误的解决
<input type="hidden" name="_csrf" value="<?=Yii::$app->request-> ...
- Mybatis学习笔记18 - 缓存
两级缓存: 一级缓存:(本地缓存):sqlSession级别的缓存.一级缓存是一直开启的:SqlSession级别的一个Map 数据库同一次会话期间查询到的数据会放在本地缓存中.以后如果需要获取相同的 ...
- U盘中毒问题解决
背景: 最近在学校的打印店里打印东西,结果过了一段时间再使用的时候发现,U盘中的文件夹都成了快捷方式,只有一个pdf文件是好的,无奈,其中有比较重要的东西,所以寻求解决办法,最终解决,为方便以后查阅, ...
- 牛客网Java刷题知识点之字节流练习之从A处复制文本文件到B处(FileReader、FileWriter )、复制文本文件的原理图解
不多说,直接上干货! CopyTextTest.java package zhouls.bigdata.DataFeatureSelection.test; import java.io.FileRe ...
- Building the main Guest Additions module [FAILED]
虚拟机中的centos7安装vbox的增强工具报错 Building the main Guest Additions module [FAILED] 查看日志发现 unable to find th ...
- go语言初始化内部结构体3中方式
package main import ( "fmt" ) type User struct { Id int Name string Age int } type Manger ...