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 ...
随机推荐
- A - Supermarket
Problem description We often go to supermarkets to buy some fruits or vegetables, and on the tag the ...
- js 全选选框与取消全选代码
设置一个全选选框和四个子选框,要实现点击全选后四个子选框选中,取消全选后四个子选框也取消.全选后点击某个子选框,全选也能取消.当四个子选框都选中时,全选框也被选择. 实现代码: <script& ...
- C# 查找、结束进程 - 通过进程名精确、模糊查找、结束进程
/// <summary> /// 根据“精确进程名”结束进程 /// </summary> /// <param name="strProcName" ...
- OpenCV: kalman滤波的代码段
序言:在我的疲劳检测工程 AviTest中!显示框为320*240,使用OpenCV的kalman滤波算法,可以实现简单的锁相追踪-实现对眼球的位置锁定. 代码如下: CvPoint Wishchin ...
- 范畴论-一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已
范畴即为结构:包含要素和转化. 范畴为高阶类型. 函子为高阶函数.函子的输入为态射.函子为建立在态射基础上的高阶函数.函子用于保持范畴间映射的结构.态射用于范畴内部的转换. 群为运算规则的约束. 自函 ...
- web开发——在网页中引用字体包(.ttf),即嵌入特殊字体
在写html时,有点时候需要显示一些特殊字体,不过这些特殊字体是系统一般不自带的,这时就需要我们自行加载要用的字体.方法如下: 1.首先在style里添加: @font-face { font-fam ...
- 通俗易懂之SpringMVC&Struts2前端拦截器详解
直接进入主题吧!一,配置Struts2的拦截器分两步走1配置对应的拦截器类:2在配置文件Struts.xml中进行配置拦截器同时在Strust2中配置拦截器类有三种方法1实现Interceptor接口 ...
- MATLAB图形界面设计(上)
参考https://www.cnblogs.com/BlueMountain-HaggenDazs/p/4307777.html 一.图形句柄 1.定义 MATLAB在创建每一个图形对象时,都会给该对 ...
- python基础1 格式化输出
转载自:https://www.cnblogs.com/fat39/p/7159881.html %用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 ...
- esp32(M5STACK) ARDUINO开发环境搭建(ubuntu)
首先去官网下载arduino https://www.arduino.cc/en/main/software 由于国产链接下载慢的缘故,所以可以采用百度网盘的方式进行下载,具体下载方法 ...