SpringMVC配置字符串绑定日期对象
来源:互联网 发布:财政部会计报表软件 编辑:程序博客网 时间:2024/05/29 06:36
1. controller中局部绑定
步骤一:编写转换器,前端字符串转化为日期对象
public class DateEditor extends PropertyEditorSupport { private final DateFormat dateFormat; private final boolean allowEmpty; private final int exactDateLength; /** * Create a new DateEditor instance, using the given DateFormat * for parsing and rendering. */ public DateEditor(DateFormat dateFormat, boolean allowEmpty) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = -1; } /** * Create a new DateEditor instance, using the given DateFormat * for parsing and rendering. */ public DateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = exactDateLength; } /** * Parse the Date from the given text, using the specified DateFormat. */ @Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) { throw new IllegalArgumentException( "Could not parse date: it is not exactly" + this.exactDateLength + "characters long"); } else { try { long dateLong = Long.parseLong(text); text = this.dateFormat.format(new Date(dateLong)); } catch (Exception ex) { // 输入文本非long型 } try { setValue(this.dateFormat.parse(text)); } catch (ParseException ex) { setValue(null); } } } /** * Format the Date as String, using the specified DateFormat. */ @Override public String getAsText() { Date value = (Date) getValue(); return (value != null ? this.dateFormat.format(value) : ""); }}
步骤二:在要转化的controller中注册
/** * 绑定日期格式 * * @param res 资源请求 * @param binder 资源绑定 */@InitBinderpublic void initBinder(HttpServletRequest res, WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class, new DateEditor(sdf, false));}
2. 配置全局日期转换器
新版的Spring3.1.x之后,配置全局转换器有所不同。
步骤一:编写全局日期转换器
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;import org.springframework.core.convert.converter.Converter;import org.springframework.util.StringUtils;public class DateConverter implements Converter<String, Date> { private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd"); private static final SimpleDateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat TIMEFORMAT2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); @Override public Date convert(String source) {// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); if (!StringUtils.hasText(source)) { return null; } try { SimpleDateFormat dateFormat = getDateFormat(source); dateFormat.setLenient(false); return dateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } return null; } /** * 功能:用本地的三种日期格式实例化出DateFormat对象 * * @param value 待解析的值 * @return DateFormat对象 */ private SimpleDateFormat getDateFormat(String value){ SimpleDateFormat dateFormat = null; Pattern patter = null; Matcher matcher = null; String formatter = null; try{ // ^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$ // yyyy-mm-dd patter = Pattern.compile("^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"); matcher = patter.matcher(value); if (matcher.matches()) { dateFormat = DATEFORMAT; formatter = "yyyy-mm-dd"; //System.out.println("yyyy-mm-dd"); return dateFormat; } // yyyy-MM-dd HH:mm:ss patter = Pattern.compile("^\\d{4}\\-\\d{1,2}\\-\\d{1,2}\\s{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$"); matcher = patter.matcher(value); if (matcher.matches()) { dateFormat = TIMEFORMAT; formatter = "yyyy-MM-dd HH:mm:ss"; //System.out.println("yyyy-MM-dd HH:mm:ss"); return dateFormat; } // yyyy/MM/dd HH:mm:ss patter = Pattern.compile("^\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$"); // yyyy-MM-dd HH:mm:ss matcher = patter.matcher(value); if (matcher.matches()) { dateFormat = TIMEFORMAT2; formatter = "yyyy/MM/dd HH:mm:ss"; //System.out.println("yyyy/MM/dd HH:mm:ss"); return dateFormat; } if (!StringUtils.hasText(formatter)) { throw new IllegalArgumentException("cannot parse value[" + value + "] to date"); } }catch(PatternSyntaxException se){ throw se; } return dateFormat; }
步骤二:在配置文件中进行配置
(1) <mvc:annotation-driven />
中配置中的AnnotationMethodHandlerAdapter
已经配置了webBindingInitializer
,
我们可以通过设置其属性conversionService
来实现自定义类型转换
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.doje.XXX.web.DateConverter" /> <!-- 转换器实现类 --> </list> </property> </bean>
(2) 修改spring service context xml配置文件中的annotation-driven,增加属性conversion-service指向新增的conversionService bean
<mvc:annotation-driven conversion-service="conversionService" />
0 0
- SpringMVC配置字符串绑定日期对象
- SpringMVC日期绑定
- springmvc命令对象绑定
- springMVC数据对象绑定
- springMVC 日期字符串统一转换为 date 对象
- springmvc配置日期转化
- SpringMVC自定义日期类型的数据绑定
- springmvc-日期类型初始绑定报错
- SpringMVC自定义日期类型的数据绑定
- SpringMVC自定义日期类型的数据绑定
- SpringMVC自定义日期格式进行参数绑定
- SpringMVC初始化参数绑定--日期格式
- springmvc JSON日期对象转换
- springmvc自定义参数绑定实现日期参数绑定
- springmvc form多对象映射 数据绑定 多对象绑定
- springmvc form多对象映射 数据绑定 多对象绑定
- springmvc配置自定义日期转换器
- SpringMVC配置全局日期转换器
- 架构师之路(1)---面向过程和面向对象
- 类间的关系
- 减少页面回流与重绘(Reflow & Repaint)
- 单表60亿记录等大数据场景的MySQL优化和运维之道
- 《从零开始学Swift》学习笔记(Day 8)——小小常量、变量大作用
- SpringMVC配置字符串绑定日期对象
- setOnItemSelectedListener与OnItemClickListener的区别
- uboot 命令构造宏分析
- STL源码剖析 [容器](十四)[stl_set.h]
- CocoaPods 安装和使用
- ArcGIS for Android示例解析之空间查询-----QueryTask
- Android中AsyncTask类与AsyncQueryHandler类的用法
- 子网划分两个例子---经典
- 数组存储在栈中还是堆中