本文主要介绍了springmvc中日期格式转换的相关知识:用来解决日期提交转换异常的问题。具有很好的参考价值。下面跟着小编一起来看下吧
解决日期提交转换异常的问题
由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对requestmapping标记的方法进行适配,并对方法中的形参进行参数绑定。在springmvc这可以在处理器适配器上自定义converter进行参数绑定。如果使用f7ba1f27e11c63617ca69c495697dd74可以在此标签上进行扩展。
1.自定义dataconvertor类, 并实现convertor接口
public class dateconverter implements converter<string, date> {
@override
public date convert(string source) {
simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
try {
return simpledateformat.parse(source);
} catch (parseexception e) {
e.printstacktrace();
}
return null;
}
}
2.在springmvc.xml配置文件中注册转换器
方法一:通过注解驱动的方式加载转换器
<!-- 配置mvc注解驱动 -->
<mvc:annotation-driven conversion-service="conversionservice"/>
<!-- 配置日期转换器 -->
<bean id="conversionservice" class="org.springframework.format.support.formattingconversionservicefactorybean">
<property name="converters">
<set>
<bean class="cn.rodge.ssm.converter.dateconverter"></bean>
</set>
</property>
</bean>
方法二:通过自定义webbinder配置(不常用)
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描带controller注解的类 -->
<context:component-scan base-package="cn.itcast.springmvc.controller" />
<!-- 转换器配置 -->
<bean id="conversionservice"
class="org.springframework.format.support.formattingconversionservicefactorybean">
<property name="converters">
<set>
<bean class="cn.itcast.springmvc.convert.dateconverter"/>
</set>
</property>
</bean>
<!-- 自定义webbinder -->
<bean id="custombinder" class="org.springframework.web.bind.support.configurablewebbindinginitializer">
<property name="conversionservice" ref="conversionservice" />
</bean>
<!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter">
<property name="webbindinginitializer" ref="custombinder"></property>
</bean>
<!-- 注解处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping"/>
<!-- 加载注解驱动 -->
<!-- <mvc:annotation-driven/> -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
<property name="viewclass" value="org.springframework.web.servlet.view.jstlview" />
<!-- jsp前缀 -->
<property name="prefix" value="/web-inf/jsp/" />
<!-- jsp后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
注意:此方法需要独立配置处理器映射器、适配器,不再使用f7ba1f27e11c63617ca69c495697dd74
以上就是springmvc中日期格式转换的示例代码详解的详细内容。