springMvc把client传过来一个String类型,转换为日期类型为例
springMvc--接受日期类型参数处理
这个问题,也即是springMvc如何进行参数类型的转换 , 以把client传过来一个String类型,转换为日期类型为例
步骤
1.controller

/**
* 接收日期类型参数
* 注意:
* springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误
* 解决办法
* 1.全局日期处理
*
*/ @RequestMapping("/test")
public String test(Date birthday){
System.out.println(birthday);
return "index";
}

2.自定义类型转换规则
SpringMvc提供了Converter接口,它支持从一个Object转换为另一个Object

/**
* 全局日期处理类
* Convert<T,S>
* 泛型T:代表客户端提交的参数 String
* 泛型S:通过convert转换的类型
*/
public class DateConvert implements Converter<String, Date> { @Override
public Date convert(String stringDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} }

3.注册自定义的类型转换类

<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionService"/> <!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConvert"/>
</set>
</property>
</bean>
<!-- 第一步: 创建自定义日期转换规则 -->
<bean id="dateConvert" class="zpark.convert.DateConvert"/>

4.地址栏访问
http://localhost:9999/date/test2?birthday=1990-01-02
参考:http://blog.csdn.net/renhui999/article/details/9837897
ps:测试了很久才成功,失败的次数太多就不记录下来了,以上只是记录了测试正确的步骤
本文转自http://www.cnblogs.com/liuconglin/p/5777879.html 感谢作者
jsp页面String类型转Controller后台Date类型
方法1.在实体中加入日期格式化注解
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
方法2.在controller中加入数据绑定代码

package com.fyh.www.pojo.user; import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder; public class LoginController { @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值 }
}

方法3.注册一个全局日期类型转化器
注册全局转化器

<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 设置Converter转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 设置多个转换器 -->
<property name="converters">
<list>
<bean class="com.fyh.www.common.mvcConverter.CustomTrimConverter"></bean>
</list>
</property>
</bean>

具体的实现代码

public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

后台date类型到前台String类型
JSP模版引擎方法:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>
Freemarker模版引擎方法:
<input id="receiveAppTime" name="receiveAppTime" type="text" value="${(bean.receiveAppTime?string('yyyy-MM-dd'))!}" />
本文转自http://www.cnblogs.com/woms/p/6037902.html 感谢作者
springMvc把client传过来一个String类型,转换为日期类型为例的更多相关文章
- 小问题,小细节要注意(string类型转换为bool类型)
一个表中的推荐字段是bit类型的,添加的时候推荐有两个值,如<asp:RadioButtonList ID="RadioButtonList1" runat="se ...
- 注意SSIS中的DT_NUMERIC类型转换为字符类型(比如DT_WSTR)时,会截断小数点前的0
我们知道SSIS中有许多数据类型,如下图所示: 但是DT_NUMERIC这个类型有个陷进要注意,我们来做个实验,随便定义一个String类型的SSIS包变量,然后打开该变量表达式窗口: 在变量表达式窗 ...
- C# 把字符串类型日期转换为日期类型(转载)
C# 把字符串类型日期转换为日期类型 来源:https://www.cnblogs.com/raincedar/p/7009243.html 方法一:Convert.ToDateTime(stri ...
- Java进阶(二十三)java中long类型转换为int类型
java中long类型转换为int类型 由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参 ...
- { MySQL基础数据类型}一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型
MySQL基础数据类型 阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己 ...
- 深度学习原理与框架-Tensorflow基本操作-mnist数据集的逻辑回归 1.tf.matmul(点乘操作) 2.tf.equal(对应位置是否相等) 3.tf.cast(将布尔类型转换为数值类型) 4.tf.argmax(返回最大值的索引) 5.tf.nn.softmax(计算softmax概率值) 6.tf.train.GradientDescentOptimizer(损失值梯度下降器)
1. tf.matmul(X, w) # 进行点乘操作 参数说明:X,w都表示输入的数据, 2.tf.equal(x, y) # 比较两个数据对应位置的数是否相等,返回值为True,或者False 参 ...
- Date类型转换为Integer类型
Date类型转换为Integer类型: Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMd ...
- java中long类型转换为int类型
由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一.强制类型转换 [java] l ...
- Qt中实现将float类型转换为QString类型
在使用Qt Creator编程时,难免会用到将float类型转换为QString类型的时候下面是我所有的方法: 1. 将QString类型转化为float类型,很简单 QString data; fl ...
随机推荐
- working hard to be a professional coder
1:read 2 : code 3 : 勤奋 4:技术栈 就前端主流技术框架的发展而言,过去的几年里发展极快,在填补原有技术框架空白和不足的同时也渐渐趋于成熟.未来前端在已经趋向成熟的技术方向上面将会 ...
- nginx connect failed (110- Connection timed out) 问题排查
首先排查 ping 下 nginx 与代理服务是否ping 的通,带端口的,telnet 下端口号是否是通的,本次遇到问题为 telnet 发现有台服务器不通,原因是端口未开放
- Visual Studio Code -VS Code
VS Code 免费开源的编辑器,支持 windows. mac. Linux. 微软出品 官网:https://code.visualstudio.com/ 下载地址:https://code.vi ...
- Linux下如何从mysql数据库里导出导入数据
https://blog.csdn.net/u012884402/article/details/47337701 一. 表的导入 1.进入数据库 mysql 数据库名 2.查看表 show tab ...
- TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
二值掩膜输出依据种类预测分支(Faster R-CNN部分)预测结果:当前RoI的物体种类为i第i个二值掩膜输出就是该RoI的损失Lmask 对于预测的二值掩膜输出,我们对每个像素点应用sigmoid ...
- 史上最大型广告欺诈活动Methbot:黑客是如何每天赚到500万美元的
根据国外安全专家的最新报告,有一群黑客正在对美国的知名企业和媒体机构进行广告欺诈活动,而这群黑客每天都可以从中赚取三百万到五百万美金. 是的,你没看错,这绝对是人类历史上最牛X的恶意广告欺诈活动!不过 ...
- 模式匹配第四弹:if case,guard case,for case
2016-06-06 7388 作者:Olivier Halligon,原文链接,原文日期:2016-05-16 译者:walkingway:校对:Cee:定稿:numbbbbb 现在我们来重新回顾下 ...
- Context Switches msdn
Context Switches https://msdn.microsoft.com/en-us/library/ms682105(VS.85).aspx The scheduler mainta ...
- MFC_2.2 编辑框和文本控件
编辑框和文本控件 1.拖控件 2.绑定变量.用户名密码编辑框控件类型.取名字.用户协议用值类型,默认CString. 设置属性.用户类型.选择mustiline TRUE. AOTO HScroll ...
- UVA - 1615 Highway(贪心-区间选点问题)
题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...