springMVC配置全局日期格式转化有三种方式
1.局部转化
在访问的controller中加上这段代码,实现日期格式转化,即实现WebBindingInitializer接口
/**
* 转换日期格式
* @param binder
* @param request
*/
* 转换日期格式
* @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>
<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));
}
}
@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接口