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类型,转换为日期类型为例的更多相关文章

  1. 小问题,小细节要注意(string类型转换为bool类型)

    一个表中的推荐字段是bit类型的,添加的时候推荐有两个值,如<asp:RadioButtonList ID="RadioButtonList1" runat="se ...

  2. 注意SSIS中的DT_NUMERIC类型转换为字符类型(比如DT_WSTR)时,会截断小数点前的0

    我们知道SSIS中有许多数据类型,如下图所示: 但是DT_NUMERIC这个类型有个陷进要注意,我们来做个实验,随便定义一个String类型的SSIS包变量,然后打开该变量表达式窗口: 在变量表达式窗 ...

  3. C# 把字符串类型日期转换为日期类型(转载)

    C# 把字符串类型日期转换为日期类型   来源:https://www.cnblogs.com/raincedar/p/7009243.html 方法一:Convert.ToDateTime(stri ...

  4. Java进阶(二十三)java中long类型转换为int类型

    java中long类型转换为int类型 由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参 ...

  5. { MySQL基础数据类型}一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型

    MySQL基础数据类型 阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己 ...

  6. 深度学习原理与框架-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 参 ...

  7. Date类型转换为Integer类型

    Date类型转换为Integer类型: Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMd ...

  8. java中long类型转换为int类型

    由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一.强制类型转换 [java] l ...

  9. Qt中实现将float类型转换为QString类型

    在使用Qt Creator编程时,难免会用到将float类型转换为QString类型的时候下面是我所有的方法: 1. 将QString类型转化为float类型,很简单 QString data; fl ...

随机推荐

  1. Android 性能优化(23)*性能工具之「Heap Viewer, Memory Monitor, Allocation Tracker」Memory Profilers

    Memory Profilers In this document Memory Monitor Heap Viewer Allocation Tracker You should also read ...

  2. WPF 添加 gif 图片

    1. 如何在wpf窗体中添加gif动态图片: https://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-wo ...

  3. ActiveMQ应用

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  4. 6.13---shiro

  5. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  6. CF817C Really Big Numbers

    思路: 二分. 实现: #include <iostream> #include <cstdio> using namespace std; typedef long long ...

  7. Elasticsearch--地理搜索

    目录 地理位置索引 空间搜索映射定义 示例 基于距离的排序 边界框过滤 距离的限制 任意地理形状搜索 点 包络线 多边形 多个多边形 把形状保存到索引中 地理位置索引 空间搜索映射定义 elastic ...

  8. JDBC性能优化

    一.使用PreparedStatement的Batch功能 参见本人一下文章:http://blog.csdn.net/lmb55/article/details/50631062 二.选择合适的光标 ...

  9. http协议对照表

    1**:请求收到,继续处理 2**:操作成功收到,分析.接受 3**:完成此请求必须进一步处理 4**:请求包含一个错误语法或不能完成 5**:服务器执行一个完全有效请求失败 100——客户必须继续发 ...

  10. (转)全文检索技术学习(二)——配置Lucene的开发环境

    http://blog.csdn.net/yerenyuan_pku/article/details/72589380 Lucene下载 Lucene是开发全文检索功能的工具包,可从官方网站http: ...