- 浏览: 25075 次
- 性别:
- 来自: 广州
最新评论
Register multiple date format for binding properities
- 博客分类:
- grails
Grails可以自动把params里面的参数绑定到domain class。有些时候为了能够绑定自定义的日期格式,例如"yyyy-MM-dd",需要register一个自己的CustomDateEditor。
有时候还有需求要绑定多种格式的日期,例如"yyyy/MM/dd"、"yyyy-MM-dd HH:mm:ss"等等,这时候就需要一个支持多种日期格式的CustomDateEditor,解决方法如下:
1. 新建一个MultipleDateEditor.groovy
2. 新建MultipleDateEditorRegistrar.groovy
3. 注册到spring beans
Reference: http://forum.springsource.org/showthread.php?78118-Multiple-Date-Formats-in-a-Form
有时候还有需求要绑定多种格式的日期,例如"yyyy/MM/dd"、"yyyy-MM-dd HH:mm:ss"等等,这时候就需要一个支持多种日期格式的CustomDateEditor,解决方法如下:
1. 新建一个MultipleDateEditor.groovy
package com.myapp import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.util.Date; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateUtils; import org.springframework.util.StringUtils; /** * Property editor for java.util.Date, supporting multiple custom input formats * and custom output formats. This class utilizes DateUtils and * DateFormatUtils of Apache Commons Lang, and has been tested for version 2.4 * but may work with older versions. * * It should be noted that the date input formats should be inserted into the * array based on the length of the string, longest to shortest. * * This is not meant to be used as system PropertyEditor but rather as * locale-specific date editor within custom controller code, parsing * user-entered number strings into Date properties of beans and rendering * them in the UI form. * * In web MVC code, this editor will typically be registered with * binder.registerCustomEditor calls in a custom initBinder method. * * @author jpreston * @see java.beans.PropertyEditorSupport * @see java.util.Date * @see org.apache.commons.lang.time.DateUtils * @see org.apache.commons.lang.time.DateFormatUtils */ public class MultipleDateEditor extends PropertyEditorSupport { /** * The date format used to format a Date into a String */ public final static String DEFAULT_OUTPUT_FORMAT = "dd/mm/yyyy"; /** * The date formats used to parse a String into a Date */ public final static String[] DEFAULT_INPUT_FORMATS = [ "dd/mm/yyyy hh:mm:ss", "dd-mm-yyyy hh:mm:ss", "dd/mm/yy hh:mm:ss", "dd-mm-yy hh:mm:ss", "dd/mm/yyyy", "dd-mm-yyyy", "dd/mm/yy", "dd-mm-yy" ]; /** The format used to convert a Date into a String */ private String outputFormat; /** An array of date formats used to convert a String into a Date */ private String[] inputFormats; /** Allow empty strings to be parsed instead of treated as null */ private boolean allowEmpty; /** * Create a new MultipleDateEditor instance using the default values */ public MultipleDateEditor() { outputFormat = MultipleDateEditor.DEFAULT_OUTPUT_FORMAT; inputFormats = MultipleDateEditor.DEFAULT_INPUT_FORMATS; allowEmpty = false; } /** * Create a new MultipleDateEditor instance using the given date * input and output formats. * * The "allowEmpty" parameter states if an empty String should be allowed for * parsing, i.e. get interpreted as null value. Otherwise, an * IllegalArgumentException gets thrown in that case. * * @param outputFormat The format used to convert a Date into a String * @param inputFormats An array of date formats used to convert a String into a Date */ public MultipleDateEditor(String outputFormat, String[] inputFormats) { this.outputFormat = outputFormat; this.inputFormats = inputFormats; allowEmpty = false; } /** * Create a new MultipleDateEditor instance using the given date * input and output formats. * * The "allowEmpty" parameter states if an empty String should be allowed for * parsing, i.e. get interpreted as null value. Otherwise, an * IllegalArgumentException gets thrown in that case. * * @param outputFormat The format used to convert a Date into a String * @param inputFormats An array of date formats used to convert a String into a Date * @param allowEmpty Allow empty strings to be parsed instead of treated as null */ public MultipleDateEditor(String outputFormat, String[] inputFormats, boolean allowEmpty) { this.outputFormat = outputFormat; this.inputFormats = inputFormats; this.allowEmpty = allowEmpty; } /** * Format the Date as String, using the specified outputFormat. * * If allowEmpty is true (default is false), and the Date is null, an empty * String will be returned; otherwise it is possible that a NPE or other * parsing exception will occur. * * @return The string value of the Date */ @Override public String getAsText() { if (allowEmpty && getValue() == null) { return ""; } return DateFormatUtils.format((Date) getValue(), outputFormat); } /** * Parse the Date from the given text, using the first matching date format * specified in the inputFormats array. * * If no matching input format is found, an IllegalArgumentException is thrown. * * If allowEmpty is true (default is false), and the String is null, the Date * will be interpreted as null; otherwise an IllegalArgumentException is thrown. * * @param text the text to convert into a java.util.Date * @throws IllegalArgumentException thrown if no matching format is found */ @Override public void setAsText(String text) throws IllegalArgumentException { try { if (StringUtils.hasText(text)) { setValue(DateUtils.parseDate(text, inputFormats)); } else { if (allowEmpty) { setValue(null); } else { throw new IllegalArgumentException( "The text specified for parsing is null"); } } } catch (ParseException ex) { throw new IllegalArgumentException("Could not parse text [" + text + "] into any available date input formats", ex); } } /** * If allowEmpty is true (default is false), and the Date is null, an empty * String will be returned; otherwise it is possible that a NPE or other * parsing exception will occur. * * If allowEmpty is true (default is false), and the String is null, the Date * will be interpreted as null; otherwise an IllegalArgumentException is thrown. * * @return whether empty strings are allowed */ public boolean isAllowEmpty() { return allowEmpty; } /** * If allowEmpty is true (default is false), and the Date is null, an empty * String will be returned; otherwise it is possible that a NPE or other * parsing exception will occur. * * If allowEmpty is true (default is false), and the String is null, the Date * will be interpreted as null; otherwise an IllegalArgumentException is thrown. * * @param allowEmpty whether empty strings should be allowed */ public void setAllowEmpty(boolean allowEmpty) { this.allowEmpty = allowEmpty; } /** * Get the date formats used to parse a String into a Date * * @return The date formats used to parse a String into a Date */ public String[] getInputFormats() { return inputFormats; } /** * Set the date formats used to parse a String into a Date. It should be * noted that the date formats should be inserted into the array based * on the length of the format, longest to shortest. * * * @param inputFormats The date formats used to parse a String into a Date */ public void setInputFormats(String[] inputFormats) { this.inputFormats = inputFormats; } /** * Get the format used to convert a Date into a String * * @return The format used to convert a Date into a String */ public String getOutputFormat() { return outputFormat; } /** * Set the format used to convert a Date into a String * * @param outputFormat The format used to convert a Date into a String */ public void setOutputFormat(String outputFormat) { this.outputFormat = outputFormat; } }
2. 新建MultipleDateEditorRegistrar.groovy
package com.myapp import org.springframework.beans.PropertyEditorRegistrar import org.springframework.beans.PropertyEditorRegistry /** * binding date properties with customer format * "yyyy-MM-dd HH:mm:ss" * "yyyy-MM-dd HH:mm" * "yyyy-MM-dd" */ public class MultipleDateEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new MultipleDateEditor("yyyy-MM-dd HH:mm:ss", ["yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd"] as String[], true)); } }
3. 注册到spring beans
beans = { customPropertyEditorRegistrar(com.nazuche.MultipleDateEditorRegistrar) }
Reference: http://forum.springsource.org/showthread.php?78118-Multiple-Date-Formats-in-a-Form
发表评论
-
Fedora 14 下grails中文乱码问题(解决方法)
2011-10-20 18:25 1144MySQL默认字符编码为latin1,因此用Grails写入数 ... -
Grails - binding a customized date format
2011-09-26 18:26 1271It has been possible to bind a ... -
Changing last login date in Grails with Spring Security
2011-09-06 14:31 1160在使用grails spring security core ... -
Grails, spring-security-core plugin:使用email登录
2011-09-06 14:27 26951. Implement the first requirem ... -
Run a Java web application within grails
2011-08-27 13:22 932Ever needed to run an existing ... -
Execute SQL Scripts in Grails Bootstrap or Integration Tests
2011-08-12 15:45 1677I recently had to execute some ... -
Grails, JQuery, and AJAX
2011-08-11 15:02 1364By now, you’re probably under t ... -
Grails - Logging from a Controller or Service
2011-08-11 14:55 998Logging informational messages ... -
grails taglib修改datePicker格式
2011-08-10 17:07 1925grails的datePicker标签很好,但是并不能设置成y ... -
grails使用build-test-data插件准备测试数据
2011-08-10 11:30 876什么?!还有专门负责 ... -
Grails, p6spy and Sql Profiler
2011-08-09 23:05 1109There are several ways to have ...
相关推荐
NULL 博文链接:https://rayoo.iteye.com/blog/1233534
THE Java™Architecture for XML Binding (JAXB) provides a fast and convenient way to bind between XML schemas and Java representations, making it easy for Java developers to incorporate XML data and ...
Java API for JSON Binding (JSON-B).zip
win32-ia-32-48_binding.node插件去掉node不报错win32-ia-32-48_binding.node插件去掉node不报错win32-ia-32-48_binding.node插件去掉node不报错win32-ia-32-48_binding.node插件去掉node不报错win32-ia-32-48_...
主要介绍了C#应用BindingSource实现数据同步的方法,需要的朋友可以参考下
WPF的binding代码实例 总结的几个binding的基础运用
Header-only C++ binding for libzmq.zip
Emacs Key binding for Delphi 7(親測),Delphi xe2(也可以用) 使用方法: 使用delphi安裝MyEmacs組件即可, 每次打開delphi時,需要在Key Mapping中去掉勾選【Buffer List】這個Module即可, 里面有一些我自己的快捷...
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
C# MVVM Binding demo
A-PUF-FSM-Binding-Scheme-for-FPGA-IP-Protection-and-Pay-per-Device-Licensing
This application demonstrates how to use the Format and Parse event handlers when data binding Windows Form controls. The Format event fires when the data is transferred the data source to the bound ...
win32-x64-67_binding.node
Cannot download linux-x64-72_binding.node/win32-x64-72_binding.node
wpf binding 的两种方法 数据库连接 并显示的两种方法
linux-x64-64_binding.node资源
However, protein–ligand interactions assume a continuum of binding strength values, also called binding affinity and predicting this value still remains a challenge. The increase in the affinity ...
win32-x64-83_binding