我一直在寻找并尝试2天。我终于选择定义一个自定义的PropertyEditorRegistar。这样,我可以修复仅适用于Double字段格式的区域设置。但是,我不认为这是最好的解决方案,因为它将应用于我的所有Double字段。但同时它做得很好。所以如果有人有更好的解决方案,我会很乐意测试它并更新我的代码。
所以这是我此设置:
1 - 创建一个实现PropertyEditorRegistrar的(如果你已经有一个,只需添加该方法所包含的代码的一部分在现有的一个)
新的Groovy类
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
public class CustomDoubleRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
//define new Double format with hardcoded Locale.ENGLISH
registry.registerCustomEditor(Double.class,
new CustomNumberEditor(Double.class,
DecimalFormat.getInstance(Locale.ENGLISH),true))
}
}
2-定义自定义注册商到CONF /弹簧/ resources.goovy(如果它不是已存在当然)
beans = {
customPropertyEditorRegistrar(CustomDoubleRegistrar)
}
3-就是这样,在GRAI LS自动数据绑定将正常工作
Test t = new Test(params);
//params contains many Double fields with dot '.' as decimal delimiter
不要犹豫,发布更好的解决方案... 感谢
编辑1
因为Grails的2.3这个解决方案不再工作。如果你仍然想使用此解决方案,则必须将此配置添加到Config.groovy中文件
grails.databinding.useSpringBinder =真
或实现新DataBinding之一。我尝试了其中的几个,但似乎没有解决小数分隔符问题。谢谢你发布答案,如果你知道如何...
EDIT 2
由于2.4及以上的,你可以定义自己的ValueConverter的Grails绕过基本的语言环境验证。请注意,您必须删除在初始帖子和编辑1中所做的更改。以下是如何实现自定义ValueConverter:
的conf /春/ resources.groovy
// Place your Spring DSL code here
beans = {
"defaultGrailsjava.lang.DoubleConverter"(DoubleValueConverter)
}
class DoubleValueConverter implements ValueConverter {
public LongValueConverter() {
}
boolean canConvert(value) {
value instanceof Double
}
def convert(value) {
//In my case returning the same value did the trick but you can define
//custom code that takes care about comma and point delimiter...
return value
}
Class<?> getTargetType() {
return Double.class
}
}
它的工作原理! :), 非常感谢你! – conca 2013-05-09 23:32:25
我正在使用grails 3.3.1,这个解决方案不适用于我的命令对象。也@BingUsing不工作。我不知道他们为什么改变旧的行为。新的一个令人厌恶:/ – pebbles 2017-12-08 14:23:43