当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC 传日期参数到后台的实例讲解

SpringMVC 传日期参数到后台的实例讲解

2019年07月19日  | 移动技术网IT编程  | 我要评论
1、注解方式,在controller层通过initbinder注解实现 @initbinder public void initbinder(httpservl

1、注解方式,在controller层通过initbinder注解实现

@initbinder
public void initbinder(httpservletrequest request,servletrequestdatabinder binder)throws exception { 
  dateformat fmt = new simpledateformat("yyyy-mm-dd"); 
  customdateeditor dateeditor = new customdateeditor(fmt, true); 
  binder.registercustomeditor(date.class, dateeditor); 
}

2、类型转换,springmvc提供了converter接口

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;
 }
}

spring.xml中配置转换器

<!-- 第一步: 创建自定义日期转换规则 -->
<bean id="dateconvert" class="xxx.xxx.dateconvert"/>

<!-- 第二步: 创建convertion-service ,并注入dateconvert-->
<bean id="conversionservice" class="org.springframework.format.support.formattingconversionservicefactorybean">
 <property name="converters">
  <set>
   <ref bean="dateconvert"/>
  </set>
 </property>
</bean>

<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionservice"/>

以上这篇springmvc 传日期参数到后台的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网