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 ...
随机推荐
- BestCoder Round #64 1001
Numbers Accepts: 480 Submissions: 1518 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- build-helper-maven-plugin
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven ...
- Jenkins 第一次访问时提示Unlock Jenkins
Jenkins启动后录入网址提示Unlock Jenkins,To ensure Jenkins is securely set up by the administrator Jenkins启动后, ...
- Java字符串拆分和字符串连接
Java字符串拆分/连接 public class LierString{ //------------------------------------------------------------ ...
- SSL证书切换
SSL证书:SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.因为配置在服务器上,也称为SSL服务器证书.SSL 证书就是遵守 SSL协议,由受信任的数字证书颁发机构CA,在验证服务 ...
- JS数组去重总结
方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 Array.prototype.distinct = function(){ var arr = th ...
- Vue.js-----轻量高效的MVVM框架(二、Vue.js的简单入门)
1.hello vue.js! (1)引入vue.js <script type="text/javascript" src="js/vue.js"> ...
- 转 Alert.log shows No Standby Redo Logfiles Of Size 153600 Blocks Available
http://blog.itpub.net/23135684/viewspace-703620/ Alert.log shows No Standby Redo Logfiles Of Size 15 ...
- leetcode 925. 长按键入
题目描述: 你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的 ...
- CentOS7/Ubuntu Linux java jdk 环境变量配置
[root@localhost sony]# tar -zxvf jdk-8u121-linux-x64.tar.gz [root@localhost sony]# .0_121 /usr/local ...