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. DFS——hdu5682zxa and leaf

    一.题目回顾 题目链接:zxa and leaf Sample Input 2 3 2 1 2 1 3 2 4 3 9 6 2 1 2 1 3 1 4 2 5 2 6 3 6 5 9   Sample ...

  2. 基于phonegap,html5,ratchet,handlebars等技术的微表情APP

    该app是由很多有意思的微表情构成的,支持40种表情,并且每种表情都有不同的状态,主要有搜索表情,分享表情,摇一摇换表情等功能.目前只支持安卓版.由前期构思,到技术选型,到界面设计,到编码测试,再到发 ...

  3. Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器

    请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...

  4. C语言单元测试

    转自http://blog.csdn.net/colin719/article/details/1420583 对于敏捷开发来说,单元测试必不可少,对于Java开发来说,JUnit非常好,对于C++开 ...

  5. PHP变量类型转换

    PHP数据类型转换 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(s ...

  6. IO流分类详细介绍和各种字节流类介绍与使用 过滤流 字节流

    Java基础笔记 – IO流分类详细介绍和各种字节流类介绍与使用 过滤流 字节流本文由 arthinking 发表于627 天前 ⁄ Java基础 ⁄ 评论数 1 ⁄ 被围观 2,036 views+ ...

  7. 使用Kibana

    Kibana基本使用 https://www.elastic.co/guide/en/kibana/6.x/tutorial-load-dataset.html https://www.elastic ...

  8. hdu6121 build a tree(树)

    题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...

  9. MySQL自增属性auto_increment_increment和auto_increment_offset

    MySQL的系统变量或会话变量auto_increment_increment(自增步长)和auto_increment_offset(自增偏移量)控制着数据表的自增列ID. mysql> sh ...

  10. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...