Converter实现Date类型转换
1.springmvc-config.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.wxy.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.wxy.convert.DateConverter" />
</set>
</property>
</bean>
</beans>
2.日期转换类DateConverter.java
package com.wxy.convert; import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateConverter implements Converter<String,Date> {
private String datePattern = "yyyy-MM-dd HH:mm:ss";
@Override
public Date convert(String source){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
try{
return simpleDateFormat.parse(source);
}catch (ParseException e){
throw new IllegalArgumentException(
"无效的日期格式,请使用这种格式:"+datePattern
);
}
}
}
3.日期控制器类DateController
package com.wxy.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
@Controller
public class DateController {
@RequestMapping("/customDate")
public String CustomDate(Date date){
System.out.println("date="+date);
return "success";
}
}
目前是不完善版本,只能控制台输出,等完善jsp页面,再修改。
Converter实现Date类型转换的更多相关文章
- springmvc Date类型转换
有时候我们会碰到这么一个问题,有一个实体类,里面有一个Date类型的数据,jsp页面传递的时间参数是String的,这就导致无法对应,springmvc无法帮我们自动封装参数到实体类中了,这里我解决的 ...
- Java,mysql String与date类型转换
String 与 date类型转换 字符串转换成日期类型: SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写 ...
- Spring时间(Date)类型转换+自定义
第一种方法:利用内置的 CustomDateEditor(不推荐,每个类都需要定义) 首先,在我们的 Controller 的 InitBinder 里面,注册 CustomEditor //首先初始 ...
- javaBean中 字符串 转 date 类型转换
1-----创建javabean 代码如下 package BeanUtils; import java.util.Date; public class Admin { private String ...
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
问题描述: java里面时间类型转换成json数据就成这样了: "createTime":{"date":30,"day":3," ...
- Spring MVC JSON 实现JsonSerializer Date类型转换
转载至:http://blog.csdn.net/lantianzhange/article/details/40920933 在Spring MVC中存在两大类的类型转换,一类是Json,一个是Sp ...
- Json序列化,date类型转换后前端显示错误的解决方案
1.前台使用Jquery解决 (1)如果我们前台使用Jquery来解决这个问题的话,那么我们首先想到的是我们如何解析这个过程呢,当然我们就想到了自己写一个JavaScript脚本来解析这个过程,当然这 ...
- Java WebService把Date类型转换成XMLGregorianCalendar
JavaEE 的WebService中的Date类型在Web应用中调set方法的时候,默认情况下,JAXB将xsd:date, xsd:time, 和xsd:dateTime映射为XMLGregori ...
- JAVA基础——时间Date类型转换
在java中有六大时间类,分别是: 1.java.util包下的Date类, 2.java.sql包下的Date类, 3.java.text包下的DateFormat类,(抽象类) 4.java.te ...
随机推荐
- 2015 多校赛 第七场 1011 (hdu 5379)
题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子 ...
- facade 模式和gateway模式的区别
原文:http://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-desi ...
- 2.0 Linux系统的安装之Fedora安装单系统(2)
2.0 Linux系统的安装之Fedora安装单系统(2) *Linux系统的安装之Fedora安装单系统 恐怕最好装的系统就是Linux系统了,或者与Windows并列.此篇教程为Fedora的单系 ...
- DeltaFish 校园物资共享平台 第四次小组会议
一.上周记录汇报 齐天扬 学习慕课HTML至14章.构建之法10-14章 李 鑫 学习制作简易的JSP页面和servlet,看完关于HTML的慕课 陈志锴 学习编制简易JSP页面和servlet, ...
- AI:模式识别的数学表示(集合—函数观点)
前言: 模式识别的定义,参考:模式识别两种方法:知识和数据 .百科定义:模式识别(英语:Pattern Recognition),就是通过计算机用数学技术方法来研究模式的自动处理和判读.我们把环境与客 ...
- OpenCV中的模板匹配/Filter2d
1.模板匹配 模板匹配是在图像中寻找目标的方法之一.Come On, Boy.我们一起来看看模板匹配到底是怎么回事. 参考链接:http://www.opencv.org.cn/opencvdoc/2 ...
- (1)dotnet开源电商系统-brnshop&brnMall 和老外开发的nopCommerce(dotnet两套电商来PK--第一篇)
一直想做电商软件,但是实在不想学PHP了,所以前后关注了这两个开源电商系统.一个是国人出品的,一个据说是俄罗斯人写得(不知道对不对).目前两个开源软件都在学习了解中,以下的博文可能会涉及到这两套系统, ...
- (转)基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
http://www.cnblogs.com/wuhuacong/p/4757984.html 最近一直很多事情,博客停下来好久没写了,整理下思路,把最近研究的基于Metronic的Bootstrap ...
- 【Web缓存机制系列】2 – Web浏览器的缓存机制-(新鲜度 校验值)
Web缓存的工作原理 所有的缓存都是基于一套规则来帮助他们决定什么时候使用缓存中的副本提供服务(假设有副本可用的情况下,未被销毁回收或者未被删除修改).这些规则有的在协议中有定义(如HTTP协议1.0 ...
- Docker 导入镜像报错:open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no such file or directory
下载好了redis的tar包,然后导入redis镜像是报错open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no s ...