springMVC配置全局日期格式转化有三种方式

1.局部转化
在访问的controller中加上这段代码,实现日期格式转化,即实现WebBindingInitializer接口

/**
* 转换日期格式
* @param binder
* @param request
*/

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

本方法只在该controller里面有效


2.全局转化配置一

在springMVC中配置全局日期转化器

<!-- 配置全局日期转化器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.babasport.core.web.CustomDateEdtor"></bean> <!--自定义日期转化类的全路径-->
</property>
</bean>


public class CustomDateEdtor implements WebBindingInitializer{
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}

3.全局转化配置二

在springMVC中配置全局日期转化器,实现converert接口
springMVC配置全局日期格式转化


springMVC配置全局日期格式转化





相关文章:

  • 2022-12-23
  • 2021-04-23
  • 2022-02-03
  • 2021-12-25
  • 2021-05-26
  • 2021-08-08
  • 2021-08-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-07-14
  • 2022-12-23
  • 2019-06-12
相关资源
相似解决方案