springmvc,可以自动将数据注入到: “name”值相同,便注入,比如String Integer 还有我们自定义的bean,比如User。

但是date类型的数据,如果前台传的是用"/"划分的日期类型,便能注入。

但是如果前台传过来是"-"划分的日期类型,不能自动注入,会报400错误。

在form表单中,input type是date,那么表单传递的date数据是"-"划分的。

如果想支持,“-”划分的date类型数据,可以自定义转换器。

方法一:不用annotation-driven

public class DateEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
return super.getAsText();
} @Override
public void setAsText(String text) throws IllegalArgumentException {
String pattern;
SimpleDateFormat dateFormat = new SimpleDateFormat();
if(text.indexOf("-")==-1){
pattern = "yyyy/MM/dd";
}else{
pattern = "yyyy-MM-dd";
}
System.out.println("pattern = " + pattern);
dateFormat.applyPattern(pattern);
try {
setValue(dateFormat.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}

  

public class MyWebBind implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
webDataBinder.registerCustomEditor(Date.class,new DateEditor());
}
}

  

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.core.MyWebBind"></bean>
</property>
</bean>

  

方法二:用annotation-driven

public class DateConverter implements Converter<String, Date> {
public Date convert(String source) {
String pattern;
SimpleDateFormat dateFormat = new SimpleDateFormat();
if(source.indexOf("-")==-1){
pattern = "yyyy/MM/dd";
}else{
pattern = "yyyy-MM-dd";
}
System.out.println("pattern = " + pattern);
dateFormat.applyPattern(pattern);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}

  

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<util:list>
<bean class="com.core.DateConverter"/>
</util:list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>

  

springmvc对于前台date类型注意点的更多相关文章

  1. 【Spring】SpringMVC中浅析Date类型数据的传递

    在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...

  2. SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用

    使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下: 方法一 . 在需要日期转换的Con ...

  3. SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换

    SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...

  4. SpringMVC处理Date类型的成员变量方法

    原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...

  5. springMVC返回json数据时date类型数据被转成long类型

    在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...

  6. 使用springmvc从页面中获取数据,然后根据获得的参数信息进行修改,如果修改的数据中含有不是基本数据类型的参数。比如传的参数中有Date类型的数据时,需要我们进行参数类型转换。

    1.1 需求 在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式. 1.2 需求分析 由于日期数据有很多格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑 ...

  7. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  8. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  9. 后台date类型转换为json字符串时,返回前台页面的是long类型的时间戳问题解决

    学习springboot框架,写个博客系统,在后台管理的日志管理中,遇到了后台查询的日期格式的结果返回到页面变成了日期的时间戳了.然后摸索了三种方法来解决.页面的显示问题如下图. 问题页面回顾: 本案 ...

随机推荐

  1. kinit

    su tf$ kinit -k -t /tmp/tf.keytab tf/admin@SINO.COM

  2. Halcon17无法加载"hdevenginecpp":找不到指定的模块

    Halcon17无法加载"hdevenginecpp":找不到指定的模块 在C#和Halcon17混合编程中,当执行private HDevEngine MyEngine = ne ...

  3. [Mac]Mac OS X中WireShark的使用,及找不到网卡问题的解决方法

    1.WireShark依赖X11: 2.默认情况下Mac OS X是不安装X11的: 因此,在Mac上安装WireShark,首先找出Mac OS 安装DVD安装X11. 安装完以后 echo $DI ...

  4. winform label去背景

    以pictureBox上面显示一个不需要背景的label为例: 1.保证label的父控件是该pictureBox: 2.label的color属性为transParent:

  5. PAT java大数 A+B和C

    题目描述: 给定区间[-, ]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=),是测试用例的个数.随后给出T组测试用例,每组占一行,顺序给出A.B和C ...

  6. python的三种控制流

    什么是控制流 >>控制代码执行顺序的语句 >>python中有哪些控制流 >>顺序结构 >>> a = 7 >>> print( ...

  7. (转载)Hadoop示例程序WordCount详解

    最近在学习云计算,研究Haddop框架,费了一整天时间将Hadoop在Linux下完全运行起来,看到官方的map-reduce的demo程序WordCount,仔细研究了一下,算做入门了. 其实Wor ...

  8. Ubuntu下Eclipse中运行Hadoop程序的参数问题

    需要统一的参数: 当配置好eclipse中hadoop的程序后,几个参数需要统一一下: hadoop安装目录下/etc/core_site.xml中 fs.default.name的端口号一定要与ha ...

  9. Generator实质

    Generator实质 来源: <http://blog.liuwanlin.info/generatorshi-zhi/>  superlin •  September 15, 2015 ...

  10. 完全解析线程池ThreadPool原理&使用

    目录 1. 简介 2. 工作原理 2.1 核心参数 线程池中有6个核心参数,具体如下 上述6个参数的配置 决定了 线程池的功能,具体设置时机 = 创建 线程池类对象时 传入 ThreadPoolExe ...