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 ...
随机推荐
- 【DP、线段树优化】琪露诺
跟去年(2017)PJ第四题几乎是一样的?/吐血 DP方程可以很简单的推出来,f[i]=max{f[k]}+a[i] 然而这样做是O(n^2)的 看一下数据,200000的话要不nlogn 要不n 由 ...
- git的常用命令。。
git的常用命令.. git help <command> 显示command的help git show 显示某次提交的内容 git show $id git co -- <f ...
- python 5:str(某一变量)(将其他数字解释为字符串)
age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...
- 努比亚 Z17(Nubia NX563J) 解锁BootLoader 并刷入recovery
工具下载链接:https://pan.baidu.com/s/1mjEzcyG 备用下载链接:https://pan.baidu.com/s/1eTdx6Zg 密码:1d3r 本篇教程教你如何傻瓜式解 ...
- 【Linux】ubuntu中怪异的vi编辑器
由于前几天一场windows系统的比特币勒索病毒,我下狠心装了Linux的ubuntu版本.可是今天在使用命令行中的vi编辑器时出现了怪异的现象:backspace不能删除,编辑模式回车随机出现字母. ...
- Percona Xtrabackup备份及恢复
1. http://www.percona.com/software/percona-xtrabackup下载并安装 2. 全量备份 a.全量备份到制定目录 innobacku ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- Redis 之string结构及命令详解
1.set key value ex 秒数 px 毫秒数 nx / xx 设置一个值,注:ex 与 px 同时存在时,取写在后面的一个为有效期,nx表示key不存时设置成功,xx表示 ...
- 【转载】java list的一些基本操作
1.list中添加,获取,删除元素 List<String> person=new ArrayList<>(); person.add("jackie"); ...
- [转载]windows下github 出现Permission denied (publickey).解决方法
今天在学习github的时候遇到了一些问题,然后爬了一会,找到了解决方法记录下来,以防忘记,当然能帮助别人最好啦! github教科书传送门:http://www.liaoxuefeng.com/ ...