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. java设计模式之装饰器模式以及在java中作用

    在JAVA I/O类库里有很多不同的功能组合情况,这些不同的功能组合都是使用装饰器模式实现的,下面以FilterInputStream为例介绍装饰器模式的使用  FilterInputStream和F ...

  2. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  3. UVA 11297 Census(二维线段树)

    Description This year, there have been many problems with population calculations, since in some cit ...

  4. 基于twemproxy和vip实现redis集群的无感知弹性扩容

    目标是实现redis集群的无感知弹性扩容 关键点 1.是无感知,即对redis集群的用户来说服务ip和port保持不变 2.弹性扩容,指的是在需要时刻可以按照业务扩大redis存储容量. 1.业务场景 ...

  5. springmvc项目搭建四-基于前端框架完善页面的数据显示

    上一篇把前端框架先放上去了,现在开始前后端进行交互,对数据进行显示. 效果如图所示...中间经历了数据显示不上去的问题,是对于spring的注解了解不够,问题及其解决可以参照上一篇问题处理... 目前 ...

  6. awk,rsync,重启,maxdepth一层目录,登录,开机自启动

    有100个日志文件,每个文件大约1G,每条日志都以 “H:i:s” 的时间格式开头,如: 05:02:04 xxx yyy zzz 因为是日志文件,所以肯定以时间为顺序的,现在可以确定的是,在某个文件 ...

  7. [剑指Offer] 27.字符串的排列

    [思路]从第一位开始,判断每一位字符的所有可能性,依此递归. class Solution { public: void PermutationHelp(vector<string> &a ...

  8. 【SVN】SVN服务器的本地搭建和使用

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  9. 【bzoj2326】[HNOI2011]数学作业 矩阵乘法

    题目描述 题解 矩阵乘法 考虑把相同位数的数放到一起处理: 设有$k$位的数为$[l,r]$,那么枚举从大到小的第$i$个数(即枚举$r-i+1$),考虑其对$Concatenate(l..r)$的贡 ...

  10. JavaScript正则表达式大全

    一.校验数字的表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和非零开头的数字:^(0|[1-9 ...