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 ...
随机推荐
- BZOJ 3831 单调队列DP
思路: 这好像是我刚学单调性的时候做的题 (我是不会告诉你 我被这题教做人了的...) i-stk[head]>k 删队头 f[stk[tail]]>f[i]||(f[stk[tail]] ...
- vue 使用localStorage保存页面变量到浏览器变量中
const STORAGE_KEY = 'todos-vuejs'//定义常量保存键值 export default{ fetch(){ return JSON.parse(window.localS ...
- UML基本关系
UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.UML的定义包括UML语义和UML表示法两个元素. UML是在开发阶 ...
- jQuery学习笔记之DOM操作、事件绑定(2)
jQuery学习笔记之DOM操作.事件绑定(2) --------------------学习目录------------------------ 4.DOM操作 5.事件绑定 源码地址: https ...
- CentOS 安装dotNetCore
如果要在CentOS上运行.net Core程序,必须安装.net Core Sdk 具体安装 方法,可以参考微软官方站点说明,非常详细: 1)百度搜索 .Net Core 2)先择CentOS版本: ...
- C#访问Win 32的一些尝试
使用C#调用Win 32 Api大部分情况下基本只涉及到参数类型的转变,但在遇到Win 32 Api返回LPVOID *lpBuff 时会遇到一些解析遍历难题.lpBuff为二维指针,*lpBuff是 ...
- (转)基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用
http://www.cnblogs.com/wuhuacong/p/4761637.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JST ...
- Java接口和Java抽象类的认识
在没有好好地研习面向对象设计的设计模式之前,我对Java接口和Java抽象类的认识还是很模糊,很不可理解. 刚学Java语言时,就很难理解为什么要有接口这个概念,虽说是可以实现所谓的多继承,可一个只有 ...
- PAT_A1144#The Missing Number
Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...
- MySQL创建临时表
drop TEMPORARY table if EXISTS temp_table; create TEMPORARY table temp_table( id int not null, usern ...