springmvc对于前台date类型注意点
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类型注意点的更多相关文章
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
- SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用
使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下: 方法一 . 在需要日期转换的Con ...
- SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换
SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...
- SpringMVC处理Date类型的成员变量方法
原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...
- springMVC返回json数据时date类型数据被转成long类型
在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...
- 使用springmvc从页面中获取数据,然后根据获得的参数信息进行修改,如果修改的数据中含有不是基本数据类型的参数。比如传的参数中有Date类型的数据时,需要我们进行参数类型转换。
1.1 需求 在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式. 1.2 需求分析 由于日期数据有很多格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑 ...
- SpringMVC返回Json,自定义Json中Date类型格式
http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...
- 【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 ...
- 后台date类型转换为json字符串时,返回前台页面的是long类型的时间戳问题解决
学习springboot框架,写个博客系统,在后台管理的日志管理中,遇到了后台查询的日期格式的结果返回到页面变成了日期的时间戳了.然后摸索了三种方法来解决.页面的显示问题如下图. 问题页面回顾: 本案 ...
随机推荐
- MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails
具体报错信息: Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `t ...
- C#数据库连接问题
最近在看C#,今天下午刚开始接触C#的数据库连接,SQL Server2008,问题如图:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名 ...
- 微信小程序小程序使用scroll-view不能使用下拉刷新的解决办法
<scroll-view class="movie-grid-container" scroll-y="true" scroll-x="fals ...
- Flink之状态之状态存储 state backends
流计算中可能有各种方式来保存状态: 窗口操作 使用 了KV操作的函数 继承了CheckpointedFunction的函数 当开始做checkpointing的时候,状态会被持久化到checkpoin ...
- 大并发量订单处理的 KafKa部署
大并发量订单处理的 KafKa部署总结 今天要介绍的是消息中间件KafKa,应该说是一个很牛的中间件吧,背靠Apache 与很多有名的中间件搭配起来用效果更好哦 ,为什么不用RabbitMQ,因为公司 ...
- Luogu3731 HAOI2017新型城市化(二分图匹配+强连通分量)
将未建立贸易关系看成连一条边,那么这显然是个二分图.最大城市群即最大独立集,也即n-最大匹配.现在要求的就是删哪些边会使最大匹配减少,也即求哪些边一定在最大匹配中. 首先范围有点大,当然是跑个dini ...
- BZOJ4475 JSOI2015子集选取(动态规划)
数据范围过大说明这个题和组合一点关系也没有,答案基本上肯定是ab的形式了.暴力打表感觉不太好写,找到当年的题面发现还有个样例是6 40 401898087,于是暴力找ab=401898087的数,发现 ...
- [CF912A]Tricky Alchemy
题意:你有a个黄水晶和b个蓝水晶,要求要x个黄水晶球(2黄),y个绿水晶球(1黄1蓝),z个蓝水晶球(3蓝),问还要多少水晶题解:模拟 C++ Code: #include<cstdio> ...
- hdu3068最长回文(Manacher算法)
简单来说这是个很水的东西.有点dp的思想吧.推荐两个博客,很详细. http://blog.csdn.net/xingyeyongheng/article/details/9310555 http:/ ...
- BZOJ4550 小奇的博弈 【Nimk游戏 + dp + 组合数】
题目 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色.最左边是白色棋子,最右边 是黑色棋子,相邻的棋子颜色不同. 小奇可以移动白色棋子,提比可以移动黑色的棋子,它们每次 ...