在上一篇SpringMVC的提交表单中,我们使用的日期为String型,可以将日期转换为Date型,然后使用initBinder函数进行显示,具体代码如下:

(1)首先更改User.java的birthday为Date型

package com.zk.domain;

import java.util.Date;

public class User {
private Integer id;
private String name;
private String age;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

(2)其次,在CommandController.java中添加initBinder方法。

package com.zk.UserController;

import java.text.SimpleDateFormat;
import java.util.Date; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController; import com.zk.domain.User; //从页面接受参数,封装javaBean的User对象
public class CommandController extends AbstractCommandController{
//指定把参数封装到那个对象。
public CommandController(){
this.setCommandClass(User.class);
}
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
// TODO Auto-generated method stub
//值被封装命令对象
User user=(User) command; ModelAndView mv=new ModelAndView();
//设置models
mv.addObject("user",user);
//指定返回视图界面
mv.setViewName("index"); return mv;
}
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
// TODO Auto-generated method stub
String birthday=request.getParameter("birthday");
if(birthday.contains("/")){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true));
}else{
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
} }

  其余与SpringMVC提交表单的代码相同,运行后得到如下运行结果:

SpringMVC-时间类型转换的更多相关文章

  1. SpringMVC 之类型转换Converter 源代码分析

    SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...

  2. springmvc的类型转换

     一.springmvc的类型转换 (一)默认情况下,springmvc内置的类型转换器只能 将"yyyy/MM/dd"类型的字符串转换为Date类型的日期 情境一: 而现在我们无 ...

  3. spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)

    spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二) >>>>>>>>>>>>>>&g ...

  4. spring参数类型异常输出,SpringMvc参数类型转换错误输出

    spring参数类型异常输出, SpringMvc参数类型转换错误输出 >>>>>>>>>>>>>>>> ...

  5. 转:SpringMVC之类型转换Converter(GenericConverter)

    转: http://blog.csdn.net/fsp88927/article/details/37692215 SpringMVC 之类型转换 Converter 1.1 目录 1.1 目录 1. ...

  6. SpringMVC 之类型转换Converter详解转载

    SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1     目录 1.1      目录 1.2     ...

  7. 数据库时间类型和 util 包下时间类型转换

    Java 中的类型 1. java.sql 包下给出三个数据库相关的日期时间类型,分别是 java.sql.Date, 表示日期,只有年月日,没有时分秒. java.sql.Time, 表示时间, 只 ...

  8. java时间类型转换 JsonValueProcessor

    问题描述: java里面时间类型转换成json数据就成这样了:"createTime":{"date":30,"day":3,"h ...

  9. SpringMVC之类型转换Converter

    (转载:http://blog.csdn.net/renhui999/article/details/9837897) 1.1     目录 1.1      目录 1.2      前言 1.3   ...

  10. springmvc传参---LocalDateTime、Date等时间类型转换

    此处定义的dateConvert用来转换Date类型,如果是LocalDate.LocalDateTime类型,则将Date类型换成相应的类型即可,注意java8的日期类型需要用Formatter格式 ...

随机推荐

  1. 剑指offer-面试题24-反转链表-链表

    /* 题目: 定义一个函数,输入链表的头结点,反转链表输出反转后链表的头节点. */ /* 思路: 记录当前节点的next和pre. 断开当前节点指向next的指针,指向pre. */ #includ ...

  2. Codeforces Round #617 (Div. 3) 补题记录

    1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...

  3. CCS 5.5下载地址http://www.dianyuan.com/bbs/1492792.html

    http://www.dianyuan.com/bbs/1492792.html https://pan.baidu.com/s/1eQtIRK2?errno=0&errmsg=Auth%20 ...

  4. 爬虫学习笔记2requests库和beautifulsoup4库学习笔记

    目录 1.requests库 1.1 安装 2.beautifulsoup4 2.1 常用方法 2.2 bs4 中四大对象种类 2.3 遍历文档树 2.4 搜索文档树 查询id=head的Tag 查询 ...

  5. 文件上传过waf的方法

    原文链接: https://www.cesafe.com/8411.html 原始请求包: ——WebKitFormBoundary2smpsxFB3D0KbA7D Content-Dispositi ...

  6. Win10安装3 —— U盘启动工具安装

    本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12364593.html 一:准备一个空U盘 U盘容量推荐至少8个G,先提前备份好 ...

  7. USACO06DEC 牛奶模式

    题意:求最长的可重叠的 K重复子串 的长度 考虑二分长度s,转化为验证性问题. 对SA进行分组.保证组内Height最小为s.这样在组内RMQ就可以任意了,因为RMQ一定是大于S的. 只要组内元素个数 ...

  8. linux - mysql 异常:ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

    问题描述 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement 备注:新安装完数据库后,在 xshell ...

  9. 获取mybaties插入记录自动增长的主键值

    首先在Mybatis Mapper文件中insert语句中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是保存主键值的属性. 例如: <in ...

  10. Java 11 新垃圾回收器 ZGC

    可伸缩.低延迟的垃圾回收器 GC 暂停时间不超过 10ms 堆管理容量范围(小M级别,大到T级别) 对应用吞吐量影响不超过15%(对比 G1) 为进一步的添加新特性和优化做基础 默认支持 Linux/ ...